Android
Spinner
Dropdown
Java
UI Development

Get spinner selected items text?

Interview Questions practice on Codemia

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

Browse interview questions

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:

java
Spinner spinner = findViewById(R.id.countrySpinner);
String selectedText = spinner.getSelectedItem().toString();

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

java
1import android.os.Bundle;
2import android.widget.ArrayAdapter;
3import android.widget.Button;
4import android.widget.Spinner;
5import android.widget.Toast;
6import androidx.appcompat.app.AppCompatActivity;
7
8public class MainActivity extends AppCompatActivity {
9
10    @Override
11    protected void onCreate(Bundle savedInstanceState) {
12        super.onCreate(savedInstanceState);
13        setContentView(R.layout.activity_main);
14
15        Spinner spinner = findViewById(R.id.countrySpinner);
16        Button button = findViewById(R.id.showSelectionButton);
17
18        String[] countries = {"Canada", "Japan", "Brazil"};
19
20        ArrayAdapter<String> adapter = new ArrayAdapter<>(
21            this,
22            android.R.layout.simple_spinner_item,
23            countries
24        );
25        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
26        spinner.setAdapter(adapter);
27
28        button.setOnClickListener(v -> {
29            String selectedText = spinner.getSelectedItem().toString();
30            Toast.makeText(this, selectedText, Toast.LENGTH_SHORT).show();
31        });
32    }
33}

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).

java
1spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2    @Override
3    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
4        String selectedText = parent.getItemAtPosition(position).toString();
5        Toast.makeText(MainActivity.this, selectedText, Toast.LENGTH_SHORT).show();
6    }
7
8    @Override
9    public void onNothingSelected(AdapterView<?> parent) {
10    }
11});

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.

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

If you use objects like that, you can safely cast:

java
Country selected = (Country) spinner.getSelectedItem();
String label = selected.toString();
String code = selected.getCode();

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 getting null or inconsistent behavior.
  • Assuming the selected item is a String when the adapter actually holds custom objects.
  • Forgetting that onItemSelected can 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
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.