Spinner value
Spinner tutorial
Android development
UI controls
Java Spinner

How to get Spinner value?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Android, a Spinner shows a compact drop-down list and keeps track of the currently selected item. Reading that selection is simple once the widget has an adapter, but there are a few related methods and lifecycle details that matter in real applications.

The core question is whether you want the selected object, its position, or an immediate callback when the user changes it. Android exposes a separate API for each of those cases.

Set Up the Spinner Correctly

Before you can read a value, the Spinner must be connected to an adapter. A basic setup in Java looks like this:

xml
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2    android:layout_width="match_parent"
3    android:layout_height="match_parent"
4    android:orientation="vertical">
5
6    <Spinner
7        android:id="@+id/citySpinner"
8        android:layout_width="match_parent"
9        android:layout_height="wrap_content" />
10</LinearLayout>
java
1import android.os.Bundle;
2import android.widget.ArrayAdapter;
3import android.widget.Spinner;
4import androidx.appcompat.app.AppCompatActivity;
5
6public class MainActivity extends AppCompatActivity {
7    private Spinner citySpinner;
8
9    @Override
10    protected void onCreate(Bundle savedInstanceState) {
11        super.onCreate(savedInstanceState);
12        setContentView(R.layout.activity_main);
13
14        citySpinner = findViewById(R.id.citySpinner);
15
16        String[] cities = {"Toronto", "Montreal", "Vancouver"};
17        ArrayAdapter<String> adapter = new ArrayAdapter<>(
18                this,
19                android.R.layout.simple_spinner_item,
20                cities
21        );
22        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
23        citySpinner.setAdapter(adapter);
24    }
25}

Once the adapter is attached, the spinner can return its current selection.

Get the Selected Value or Index

If the adapter holds strings, getSelectedItem() is usually the method you want:

java
String selectedCity = citySpinner.getSelectedItem().toString();
int selectedIndex = citySpinner.getSelectedItemPosition();
long selectedId = citySpinner.getSelectedItemId();

These methods answer slightly different questions:

  • 'getSelectedItem() returns the selected object'
  • 'getSelectedItemPosition() returns the zero-based position'
  • 'getSelectedItemId() returns the adapter item id'

The index is useful for branching logic, while the object is better when you need the displayed value or a backing model instance.

React When the User Changes the Selection

If the app should update immediately when the user picks an option, use OnItemSelectedListener:

java
1import android.view.View;
2import android.widget.AdapterView;
3import android.widget.Toast;
4
5citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
6    @Override
7    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
8        String selectedCity = parent.getItemAtPosition(position).toString();
9        Toast.makeText(MainActivity.this, selectedCity, Toast.LENGTH_SHORT).show();
10    }
11
12    @Override
13    public void onNothingSelected(AdapterView<?> parent) {
14        // No selection to handle.
15    }
16});

This is the right place for dependent UI updates such as changing a form, loading data for the chosen item, or enabling a button only for certain selections.

Use Custom Objects When the UI Label Is Not the Real Value

Real applications often need more than a string label. You might display a city name but actually need a city code or database id. In that case, put custom objects in the adapter and override toString() so the spinner still shows readable text:

java
1public class City {
2    private final String name;
3    private final String code;
4
5    public City(String name, String code) {
6        this.name = name;
7        this.code = code;
8    }
9
10    public String getCode() {
11        return code;
12    }
13
14    @Override
15    public String toString() {
16        return name;
17    }
18}

Then retrieve the real object:

java
City selectedCity = (City) citySpinner.getSelectedItem();
String code = selectedCity.getCode();

This keeps the UI simple while preserving access to structured data.

Common Pitfalls

One common mistake is calling getSelectedItem() before the adapter is assigned. The spinner has no meaningful value yet, so the result can be null or misleading.

Another issue is treating the first onItemSelected callback as proof that the user interacted with the spinner. Android often fires that callback during initialization for the default item.

Developers also forget that getSelectedItem() returns Object. If the adapter holds custom models, cast carefully and make sure the adapter data type matches what the code expects.

Summary

  • Attach an adapter before reading a spinner selection.
  • Use getSelectedItem() for the selected object and getSelectedItemPosition() for the index.
  • Add OnItemSelectedListener when the app should react immediately to changes.
  • Use custom objects in the adapter when the visible label and the real stored value should be different.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.