304

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Sam Dutton
  • 14,775
  • 6
  • 54
  • 64

7 Answers7

638
Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
dodo
  • 6,428
  • 2
  • 14
  • 2
  • 14
    "Value" is pretty ambiguous here. This retrieves the current spinner item title (as in, the String that is displayed to the user), but not its value if you mapped an int array onto the spinner for example. – A. Steenbergen Feb 05 '15 at 13:58
  • 1
    @Doge to get value of selected item like integer value for example we should make int[] valuesArray and file it in onCreate function maybe from arrays.xml so we can use valuesArray[(int)mySpinner.getSelectedItemId()] to get integer value – Belal mazlom Apr 22 '15 at 05:26
137

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) {
    }
});
Erich Douglass
  • 51,744
  • 11
  • 75
  • 60
  • Thanks -- that worked, though I needed to add the void return type to the methods. – Sam Dutton Dec 23 '09 at 10:43
  • 7
    Is there really no way to achieve this without relying on the item's position? – Vinz Mar 01 '11 at 21:50
  • 10
    Not a very useful solution if you want to read the spinner value without relying on a selection event. – Johann Nov 07 '13 at 14:38
  • Don't onItemSelected and onNothingSelected need @Override above them? – Patrick Dec 19 '13 at 18:23
  • 3
    @Patrick The annotation Override is never required it just tells the compiler "Can you check if this override is done correctly?" – SiXoS Aug 11 '14 at 19:52
  • @SiXoS Interesting, I never knew that. But, it looks like the best practice is to use Override notation: stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why – Patrick Aug 11 '14 at 22:40
  • 1
    What about getting the default selected item when no change performed on it? @dodo's one is the most proper. – Aritz Oct 14 '14 at 10:31
56

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

<resources>
    <string-array name="size_entries">
        <item>Small</item>
        <item>Medium</item>
        <item>Large</item>
    </string-array>

    <string-array name="size_values">
        <item>12</item>
        <item>16</item>
        <item>20</item>
    </string-array>
</resources>

and this is your spinner:

<Spinner
    android:id="@+id/size_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/size_entries" />

Then in your code to get the entries:

Spinner spinner = (Spinner) findViewById(R.id.size_spinner);
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
lenooh
  • 10,364
  • 5
  • 58
  • 49
16

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
15
View view =(View) getActivity().findViewById(controlId);
Spinner spinner = (Spinner)view.findViewById(R.id.spinner1);
String valToSet = spinner.getSelectedItem().toString();
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
gilix
  • 545
  • 10
  • 14
12

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.

Matt Logan
  • 5,886
  • 5
  • 31
  • 48
5

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

 mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
            selectedSize=adapterView.getItemAtPosition(position).toString();
wafaa hilmy
  • 61
  • 1
  • 3