15

I have a Spinner in spinnerMode="dropdown" mode. Instead of the preselected first item, I want to show the user a hint, so that there is no default selection (like »Please select an item«)

This is the UI I got:

the current UI

and this is the UI I want to achive:

the UI I want to achive

I figured that the EditText widget has an android:hint attribute, but not the Spinner widget and setting it doesn't bring me the the UI I want. This is an Android 4.x-only app, so I don't have to hassle with any pre-4.0 compatibility stuff.

Jens Kohl
  • 5,899
  • 11
  • 48
  • 77
  • 1
    whether **android:prompt="@String/select"** will work or not.? – Sahil Mahajan Mj Dec 14 '12 at 11:26
  • 1
    Sadly, no: the prompt is for the dropdown which appears after taping the `Spinner` widget. And further it doesn't work if one uses the Holo-Themes which are new in Android 4.x. – Jens Kohl Dec 14 '12 at 11:29
  • The spinner adapter has different methods, one for dropdown view and ane for listview with all options. So, when nothing is selected you adapter returns this TextView by default, else the item selected. – S.D. Dec 14 '12 at 12:02
  • 1
    @izaakcito I'll probably go with this solution: http://stackoverflow.com/a/12386866/408150. Once I finished it, I'll update this question with my solution. – Jens Kohl Dec 18 '12 at 12:12
  • https://android--code.blogspot.in/2015/08/android-spinner-hint.html another good tutorial – Prabs Aug 16 '17 at 10:37

1 Answers1

24

I haven't found an easy and clean solution yet, only this workaround using custom adapters and a custom item class:

First, we need a class for the spinner item content:

class SpinnerItem {
        private final String text;
        private final boolean isHint;

        public SpinnerItem(String strItem, boolean flag) {
            this.isHint = flag;
            this.text = strItem;
        }

        public String getItemString() {
            return text;
        }

        public boolean isHint() {
            return isHint;
        }
    }

Then our adapter class:

class MySpinnerAdapter extends ArrayAdapter<SpinnerItem> {
        public MySpinnerAdapter(Context context, int resource, List<SpinnerItem> objects) {
            super(context, resource, objects);
        }

        @Override
        public int getCount() {
            return super.getCount() - 1; // This makes the trick: do not show last item
        }

        @Override
        public SpinnerItem getItem(int position) {
            return super.getItem(position);
        }

        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }

    }

Finally we use the workaround like this:

ArrayList<SpinnerItem> items = new ArrayList<SpinnerItem>();
        items.add(new SpinnerItem("Item 1", false));
        items.add(new SpinnerItem("Item 2", false));
        items.add(new SpinnerItem("HINT", true)); // Last item 

        MySpinnerAdapter adapter = new MySpinnerAdapter(this, android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setSelection(items.size() - 1);

Then you can use the flag from the SpinnerItem class to set text color for that item or whatever.

IsaacCisneros
  • 1,953
  • 2
  • 20
  • 30
  • nice! I like this work-around. Question: what is the color of the hint text in an edit text ? – Someone Somewhere Mar 06 '13 at 20:48
  • looks like the text color of a hint in an edit text is `Color.rgb(148, 150, 148)` – Someone Somewhere Mar 06 '13 at 22:17
  • to set the color of the hint text, I implemented the `AdapterView.OnItemSelectedListener` class and within `onItemSelected` it performs `SpinnerItem data = items.get(position);` followed by `if (data.isHint() {do stuff}` – Someone Somewhere Mar 06 '13 at 22:20
  • 3
    Thanks that worked like a charm..:) one more thing no need to create separate class (Like SpinnerItem if u wanna use List – Kalpesh Lakhani May 15 '13 at 05:14
  • 3
    There something wrong with your adapter .. is shows me the package name insted of Values that i gibe to the list .... – Oussaki Apr 10 '14 at 23:37
  • @Oussaki yap i have also faced the same issue ??? are you found the solution ? – chhameed May 26 '14 at 12:47
  • 1
    @chhameed yeap i found the solution for this problem .. just a little change in the adapter ... by setting the count of the items = count-1 and add in the list item the value that you want by default to show up ... it works for me now .. and try to not use his class of item , just use the default items ... that all . if any problem i can feed you with my code . regards . – Oussaki Jun 04 '14 at 12:35
  • This doesn't works in orientation changed. It selects second last item in the list – NinjaCoder Jul 28 '14 at 21:37
  • hint text not displaying properly it shows the cut texts like here http://stackoverflow.com/questions/14188815/custom-spinner-textview-is-being-cut-off but if i select other item like item.size() - 2 it works fine – umerk44 Nov 28 '15 at 07:30
  • @Oussaki can you share the code on how you fixed it? – Diego Rivera Nov 30 '20 at 04:29
  • @DiegoRivera Read my last comment. – Oussaki Dec 06 '20 at 11:28