Skip to content

fix non-selected state and set hint text #153

@mjlovelf

Description

@mjlovelf

you need download code and change two file

1.change MaterialSpinner.java
first change init:

   private int selectedIndex = -1;
////second  change    setAdapterInternal :
private void setAdapterInternal(@NonNull MaterialSpinnerBaseAdapter adapter) {
   boolean shouldResetPopupHeight = listView.getAdapter() != null;
   adapter.setHintEnabled(!TextUtils.isEmpty(hintText));
   listView.setAdapter(adapter);
   if (selectedIndex >= adapter.getCount()) {
     selectedIndex = 0;
   }
   if (adapter.getItems().size() > 0 && selectedIndex >= 0) {
     if (nothingSelected && !TextUtils.isEmpty(hintText)) {
       setText(hintText);
       setHintColor(hintColor);
     } else {
       setTextColor(textColor);
       setText(adapter.get(selectedIndex).toString());
     }
   } else {
     String text = "please select";
     if(!TextUtils.isEmpty(hintText)){
       text = hintText;
     }
     setText(text);
   }
   if (shouldResetPopupHeight) {
     popupWindow.setHeight(calculatePopupWindowHeight());
   }
 }
  1. in xml file, set hint text
<xxxxxx.MaterialSpinner
                        android:id="@+id/spinner"
                        android:layout_width="match_parent"
                        android:layout_height="44dp"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="10dp"
                        android:elevation="1dp"
                        app:ms_hint="custom hint text"
                        android:textColor="#D9000000" />

now success!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions