Get spinner selected items text?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To get the selected text from an Android Spinner, you usually read the current selected item from the adapter and convert it to a string. The exact line is simple, but the useful part is knowing when to read it, what type the adapter returns, and why onItemSelected sometimes fires earlier than expected.
Core Sections
The direct way to read the selected text
If your spinner is backed by strings, the simplest form is:
That works after the spinner has an adapter and a valid selection. If no custom object type is involved, toString() is usually enough.
A complete example with ArrayAdapter
This pattern is appropriate when you only need the value when the user presses another button or submits a form.
Getting the text inside onItemSelected
If you want the text immediately when the user changes the spinner value, use the selection callback. In that case, you can read from parent.getItemAtPosition(position).
This is often clearer than asking the spinner again for its current value, because the callback already gives you the exact selected position.
When the adapter contains custom objects
Many apps bind a spinner to model objects, not raw strings. In that case, getSelectedItem() returns the object instance, and the displayed text depends on its toString() implementation or a custom adapter.
If you use objects like that, you can safely cast:
That is better than parsing display text back into application data.
Why selection callbacks can feel misleading
onItemSelected can fire during initial setup when the spinner receives its default selection. That surprises a lot of developers who only expect the callback after user interaction. If you only want user-driven changes, add a small guard or wait until after initial binding is complete.
Also remember that onNothingSelected is rarely used with a normal spinner. In most cases there is always one active item once the adapter is attached.
Common Pitfalls
- Calling
getSelectedItem()before setting the adapter and gettingnullor inconsistent behavior. - Assuming the selected item is a
Stringwhen the adapter actually holds custom objects. - Forgetting that
onItemSelectedcan fire during initialization, not only after a user tap. - Using display text as the real data key instead of storing an object with stable fields.
- Ignoring the possibility that
toString()on a custom object returns an unhelpful default class name.
Summary
- Use
spinner.getSelectedItem().toString()when the spinner is backed by plain strings. - Inside
onItemSelected,parent.getItemAtPosition(position)is the clearest source of the selected value. - With custom objects, cast the selected item and read real fields instead of relying only on display text.
- Expect the selection callback to run during initial setup unless you guard against it.
- The simplest solution depends on when you need the value: on submit or immediately on selection.
Related reading
- Get spring application environment in thymeleaf
- Get the length of a String
- Get the POST request body from HttpServletRequest
- Get type of a generic parameter in Java with reflection
- get string value from UISegmentedControl
- Get the current displaying UIViewController on the screen in AppDelegate.m
- Get Value of a Edit Text field
- Get value of a public static field via reflection

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.