2

Equal to the "hint" option in Edittext, I need a default text in a Spinner. So far, I used the "prompt" to a string value, the text appears as a heading of the popping up dialogue, but not as a default one. her is the spinner in the xml:

    <Spinner
        android:id="@+id/..."
        style="@style/spinner"
        android:background="@drawable/..."
        android:spinnerMode="dialog"
        android:prompt="@string/..."/>

And in the .java:

    Spinner categories = (Spinner) findViewById(R.id.spinnerCategories);
    categories.setOnItemSelectedListener(this);

    List<String> categoriesList = new ArrayList<String>();
    categoriesList.add("....");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoriesList);
    categories.setAdapter(dataAdapter);
    dataAdapter.setDropDownViewResource
                      (android.R.layout.simple_spinner_dropdown_item);
    categories.setPrompt("TEXT");
user6456773
  • 381
  • 2
  • 10
  • 18

2 Answers2

0

I need a default text in a Spinner

There is no default text in a Spinner. The Spinner always shows the selected item. This will be position 0 if you do not specify otherwise via setSelection().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Try this solution

    categoriesList.add("Select Category");
    categoryAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, categoriesList){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = super.getView(position, convertView, parent);
            if (position == getCount()) {
                ((TextView)v.findViewById(android.R.id.text1)).setText("");
                ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
            }

            return v;
        }

        @Override
        public int getCount() {
            return super.getCount()-1; // you dont display last item. It is used as hint.
        }

    };
RStack
  • 230
  • 4
  • 16