Android Spinner Get the selected item change event
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Android, a Spinner reports selection changes through AdapterView.OnItemSelectedListener. The main wrinkle is that onItemSelected can fire during initial setup, so production code often needs to distinguish between the first automatic callback and a genuine user-triggered change.
Set Up The Spinner With An Adapter
A spinner needs an adapter that provides the display values:
Once the adapter is attached, the widget knows what items it can show and select.
Listen For Selection Changes
The standard way to react to changes is setOnItemSelectedListener:
Inside onItemSelected, you can use:
- '
positionfor the numeric index' - '
idfor the row id supplied by the adapter' - '
parent.getItemAtPosition(position)for the selected value'
That callback is the spinner equivalent of a selection-change event.
Handle The Initial Callback Carefully
Many developers are surprised that onItemSelected may run as soon as the spinner is initialized, before the user taps anything. If you only want user-driven changes, add a guard:
This is a pragmatic fix for screens where the default value should not immediately trigger filtering, network calls, or form updates.
Use Stronger Models For Real Data
For quick demos, strings are fine. In real apps, spinner items often represent objects:
Because ArrayAdapter uses toString() for display, this lets you keep both a human-readable label and an internal id:
That pattern is safer than trying to map spinner positions back to database ids manually.
Persist Selection Across Recreation
If the screen is recreated, for example after rotation, restore the selected position so the UI stays consistent:
And later:
That matters because selection-driven forms can feel broken if the spinner silently resets.
If the spinner controls other UI sections, consider centralizing the selection handling in one method instead of duplicating logic inside the listener. That keeps the initialization path and the user-change path consistent.
Common Pitfalls
One common mistake is assuming onItemSelected only fires after a user click. It often fires during initialization too.
Another issue is reading the wrong value from the adapter and treating the integer position as the business id.
A third problem is modifying the spinner dataset and forgetting to notify the adapter, which makes the visible list and internal data drift apart.
Finally, when form logic depends on selection, developers sometimes trigger expensive work from every callback without guarding against duplicate initial calls.
Summary
- Use
setOnItemSelectedListenerto react to spinner selection changes. - Read the selected value with
getItemAtPosition(position). - Expect one callback during initial setup unless you guard against it.
- Prefer real model objects over raw strings when the selection maps to ids or records.
- Save and restore the selected position when screen recreation matters.

