Set selected item of spinner programmatically

Boardy picture Boardy · Jun 17, 2012 · Viewed 421.9k times · Source

I am working on an android project and I am using a spinner which uses an array adapter which is populated from the database.

I can't find out how I can set the selected item programmatically from the list. For example if, in the spinner I have the following items:

  • Category 1
  • Category 2
  • Category 3

How would I programmatically make Category 2 the selected item when the screen is created. I was thinking it might be similar to c# I.E Spinner.SelectedText = "Category 2" but there doesn't seem to be any method similar to this for Android.

Answer

Arun George picture Arun George · Jun 17, 2012

Use the following: spinnerObject.setSelection(INDEX_OF_CATEGORY2).