How to get Spinner value?

The question:

In Android, I am trying to get the selected Spinner value with a listener.

What is the best way to get the spinner’s value?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Spinner mySpinner = (Spinner) findViewById(;
String text = mySpinner.getSelectedItem().toString();

Method 2

The Spinner should fire an “OnItemSelected” event when something is selected:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        Object item = parent.getItemAtPosition(pos);
    public void onNothingSelected(AdapterView<?> parent) {

Method 3

Say this is your xml with spinner entries (ie. titles) and values:

    <string-array name="size_entries">

    <string-array name="size_values">

and this is your spinner:

    android:entries="@array/size_entries" />

Then in your code to get the entries:

Spinner spinner = (Spinner) findViewById(;
String size = spinner.getSelectedItem().toString(); // Small, Medium, Large

and to get the values:

int spinner_pos = spinner.getSelectedItemPosition();
String[] size_values = getResources().getStringArray(R.array.size_values);
int size = Integer.valueOf(size_values[spinner_pos]); // 12, 16, 20

Method 4

Yes, you can register a listener via setOnItemSelectedListener(), as is demonstrated here.

Method 5

View view =(View) getActivity().findViewById(controlId);
Spinner spinner = (Spinner)view.findViewById(;
String valToSet = spinner.getSelectedItem().toString();

Method 6

If you already know the item is a String, I prefer:

String itemText = (String) mySpinner.getSelectedItem();

Calling toString() on an Object that you know is a String seems like a more roundabout path than just casting the Object to String.

Method 7

add setOnItemSelectedListener to spinner reference and get the data like that`

 mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment