Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hint appears as an item to choose #42 #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class MaterialSpinner extends AppCompatSpinner implements ValueAnimator.A
private boolean enableFloatingLabel;
private boolean alwaysShowFloatingLabel;
private boolean isRtl;
private boolean isHintSelectable;

private HintAdapter hintAdapter;

Expand Down Expand Up @@ -183,6 +184,7 @@ private void initAttributes(Context context, AttributeSet attrs) {
isRtl = array.getBoolean(R.styleable.MaterialSpinner_ms_isRtl, false);
mHintView = array.getResourceId(R.styleable.MaterialSpinner_ms_hintView, android.R.layout.simple_spinner_item);
mDropDownHintView = array.getResourceId(R.styleable.MaterialSpinner_ms_dropDownHintView, android.R.layout.simple_spinner_dropdown_item);
isHintSelectable = array.getBoolean(R.styleable.MaterialSpinner_ms_isHintSelectable, true);

String typefacePath = array.getString(R.styleable.MaterialSpinner_ms_typeface);
if (typefacePath != null) {
Expand Down Expand Up @@ -385,7 +387,7 @@ private int prepareBottomPadding() {
}

private boolean isSpinnerEmpty() {
return (hintAdapter.getCount() == 0 && hint == null) || (hintAdapter.getCount() == 1 && hint != null);
return (hintAdapter.getCount() == 0 && (hint == null || !isHintSelectable)) || (hintAdapter.getCount() == 1 && hint != null);
}

/*
Expand Down Expand Up @@ -791,6 +793,16 @@ public boolean isRtl() {
return isRtl;
}


public void setHintSelectable(boolean isHintSelectable){
this.isHintSelectable = isHintSelectable;
invalidate();
}

public boolean isHintSelectable(){
return isHintSelectable;
}

/**
* @deprecated {use @link #setPaddingSafe(int, int, int, int)} to keep internal computation OK
*/
Expand Down Expand Up @@ -947,7 +959,9 @@ private View buildView(int position, View convertView, ViewGroup parent, boolean
}

private View getHintView(final View convertView, final ViewGroup parent, final boolean isDropDownView) {

if(isDropDownView && !isHintSelectable){
return new View(mContext);
}
final LayoutInflater inflater = LayoutInflater.from(mContext);
final int resid = isDropDownView ? mDropDownHintView : mHintView;
final TextView textView = (TextView) inflater.inflate(resid, parent, false);
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<attr name="ms_isRtl" format="boolean"/>
<attr name="ms_hintView" format="reference"/>
<attr name="ms_dropDownHintView" format="reference"/>
<attr name="ms_isHintSelectable" format="boolean"/>

</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import fr.ganfra.materialspinner.MaterialSpinner;
Expand All @@ -22,6 +24,7 @@ public class MainActivity extends AppCompatActivity {
MaterialSpinner spinner5;
MaterialSpinner spinner6;
MaterialSpinner spinner7;
MaterialSpinner spinner8;

private boolean shown = false;

Expand All @@ -43,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
initSpinnerScrolling();
initSpinnerHintAndCustomHintView();
initEmptyArray();

initHintNotSelectable();
}

private void initSpinnerHintAndCustomHintView() {
Expand Down Expand Up @@ -85,6 +88,25 @@ private void initEmptyArray() {
spinner7.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, emptyArray));
}

private void initHintNotSelectable(){
spinner8 = findViewById(R.id.spinner8);
spinner8.setAdapter(adapter);
spinner8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(spinner8.getSelectedItem() != null) {
Log.d("SELECTED_ITEM", spinner8.getSelectedItem().toString());
Log.d("SELECTED_ITEM", ITEMS[i]);
}
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Log.d("SELECTED_ITEM", "No thing selected");
}
});
}

public void activateError(View view) {
if (!shown) {
spinner4.setError(ERROR_MSG);
Expand Down
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<fr.ganfra.materialspinner.MaterialSpinner
android:id="@+id/spinner8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ms_hint="hint"
app:ms_isHintSelectable="false"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down