How to set Spinner Default by its Value instead of Position?

If you are setting the spinner values by ArrayList or Array you can assign the spinner’s selection by using the value’s index.

String myString = "some value"; //the value you want the position for

ArrayAdapter myAdapter = (ArrayAdapter) mySpinner.getAdapter(); //cast to an ArrayAdapter

int spinnerPosition = myAdapter.getPosition(myString);

//set the default according to the value
spinner.setSelection(spinnerPosition);

see the link How to set selected item of Spinner by value, not by position?

Also, you can avoid the temporary integer variable “spinnerPosition ” by directly using the method:

getPosition(String item)

Then the assigning code will be:

//set the default according to the value
spinner.setSelection(myAdapter.getPosition(myString));

Leave a Comment