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 non-selected state and set hint text #153

Open
mjlovelf opened this issue Mar 12, 2024 · 0 comments
Open

fix non-selected state and set hint text #153

mjlovelf opened this issue Mar 12, 2024 · 0 comments

Comments

@mjlovelf
Copy link

mjlovelf commented Mar 12, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant