add hint in Spinner, after clicking spinner hint not show in spinner as a item (Hint in Spinner) android studio java
public class HintAdapter extends ArrayAdapter<String> {
public HintAdapter(@NonNull Context context, int resource) {
super(context, resource);
}
public HintAdapter(@NonNull Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
public HintAdapter(@NonNull Context context, int resource, @NonNull String[] objects) {
super(context, resource, objects);
}
public HintAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull String[] objects) {
super(context, resource, textViewResourceId, objects);
}
public HintAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
super(context, resource, objects);
}
public HintAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public int getCount() {
// don't display last item. It is used as hint.
int count = super.getCount();
return count > 0 ? count - 1 : count;
}
}
2...create a Array in which you Store your Spinner items
ArrayList<String> SpinneritemArray = new ArrayList<String>();
SpinneritemArray .add("item 1");
SpinneritemArray .add("item 2");
SpinneritemArray .add("item 3");
SpinneritemArray .add("item 4");
SpinneritemArray .add("item 5");
SpinneritemArray .add("Here write your hint");
HintAdapter hintAdapter=new HintAdapter(MainActivity.this,android.R.layout.simple_spinner_dropdown_item,SpinneritemArray);
yourSpinner.setAdapter(hintAdapter);
///it show last item of Spinner as a hint but not show in Spinner as a item
yourSpinner.setSelection(hintAdapter.getCount());
///selected item of Spiner we can assign in String variable like that, also.
String spinnerSelectedItem= yourSpinner.getSelectedItem().toString();
Comments
Post a Comment