Set selected item of spinner programmatically
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Programmatically selecting a Spinner item is common when restoring user preferences, editing existing records, or reacting to data from another screen. The tricky part is timing: the adapter must be ready before selection, and the selection callback can fire even when the user did not tap anything. A robust implementation handles both concerns explicitly.
Basic Selection by Index
If your list is static and available on screen creation, you can set selection right after assigning the adapter. This is the simplest case and works well for fixed lists such as status values or simple options.
Use the two-argument version setSelection(position, animate) when you want predictable UI behavior. Setting animate to false avoids unnecessary transition effects during initial screen load.
Selection by Value with Dynamic Data
In real apps, spinner data often comes from network or database calls. In that flow, you usually know a value, not an index, so you must find the matching position after data arrives.
The key rule is simple: do not call setSelection before the adapter has data. If selection happens too early, Android silently keeps position zero, which looks like random behavior during testing.
Managing Listener Side Effects
onItemSelected can fire on first bind. If that callback triggers network requests or form recalculations, screen initialization can become noisy and expensive. A guard flag keeps startup clean.
If you need Java in a legacy codebase, the same principles apply.
Common Pitfalls
- Calling
setSelectionbefore adapter data is loaded. Fix by setting selection only after adapter assignment and data readiness. - Assuming selection callback means user action. Fix by guarding the initial callback when screen setup should not trigger business logic.
- Selecting by index from stale assumptions. Fix by selecting by value when data can be reordered or filtered.
- Not handling missing values. Fix by checking index result and defining a fallback option.
- Losing state on rotation. Fix by storing selected key in ViewModel or saved state and reapplying after data load.
Summary
- Programmatic Spinner selection is reliable when timing and callbacks are handled intentionally.
- Use index-based selection for static lists and value-based selection for dynamic lists.
- Set selection only after adapter data is available.
- Guard
onItemSelectedduring startup if initialization should be side-effect free. - Persist the selected key and reapply it after configuration changes.
Related reading
- Set style for TextView programmatically
- Set TextView text from html-formatted string resource in XML
- Set the layout weight of a TextView programmatically
- Set the maximum character length of a UITextField
- Set the maximum character length of a UITextField in Swift
- Set the maximum character length of a UITextField in Swift
- Set transparent background of an imageview on Android
- Set UIButton title UILabel font size programmatically
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.