How to get Spinner value?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
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:
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:
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:
Then retrieve the real object:
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 andgetSelectedItemPosition()for the index. - Add
OnItemSelectedListenerwhen 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
- How to get Spring RabbitMQ to create a new Queue?
- How to get status code from webclient?
- How to get the concrete class name as a string?
- How to get the current date/time in Java
- How to get text in a CATextLayer to be clear
- How to get the absolute coordinates of a view
- How to get the current time in YYYY-MM-DD HHMISec.Millisecond format in Java?
- How to get the current working directory in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.