In Android user interfaces, a Spinner provides a simple way to display a drop-down list of selectable options. When the user taps the Spinner, a list of choices appears in a dialog or drop-down, and the selected item is reflected in the UI.
This guide explains how to use Spinner in Android, populate it with data, handle item selections, and implement UI feedback that fits real-world app requirements.
What Is a Spinner in Android?
A Spinner is a widget that displays one choice at a time from a list and opens a drop-down menu when tapped. It is similar to an HTML <select> element:
- Shows a selected value
- Lets the user pick one option
- Commonly used for configuration, forms, settings, and filters
When to Use Spinner
Use a Spinner when:
- The list of choices is moderate (not too long)
- You need to conserve screen space
- Only one option can be selected at a time
For very long lists or dynamic search, consider a RecyclerView with search instead.
Add Spinner in Layout XML
Define the Spinner in your layout (e.g., activity_main.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This places a Spinner that fills the screen width.
Prepare Data for Spinner
You need a list of values for the Spinner to display. This can come from resources or code:
Example — String Array in Code
String[] countries = {"Select Country", "USA", "Canada", "India", "Australia"};
Populate Spinner with ArrayAdapter
Step 1 — Create Adapter
In your Activity:
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_spinner_item,
countries
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Key Points:
android.R.layout.simple_spinner_itemdefines the Spinner closed-view layoutsimple_spinner_dropdown_itemdefines how items appear when the list opens
Handle Item Selection
To handle user selection, implement the OnItemSelectedListener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String selectedItem = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this,
"Selected: " + selectedItem,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Optional, no action
}
});
This callback runs when the user selects an option.
Use position to access the selected index.
Using String Resources
For localization and cleaner code, store Spinner values in res/values/strings.xml:
<string-array name="country_array">
<item>Select Country</item>
<item>USA</item>
<item>Canada</item>
<item>India</item>
<item>Australia</item>
</string-array>
Then use:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.country_array,
android.R.layout.simple_spinner_item
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
This supports localization and resource re-use.
Material-Style Spinner
Using Material Components yields consistent theming:
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="@+id/material_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"/>
Wrap it with TextInputLayout for a drop-down label:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select Country">
<AutoCompleteTextView
android:id="@+id/material_spinner"
android:layout_width="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
Populate with:
String[] countries = getResources().getStringArray(R.array.country_array);
ArrayAdapter<String> matAdapter = new ArrayAdapter<>(
this,
R.layout.support_simple_spinner_dropdown_item,
countries
);
materialSpinner.setAdapter(matAdapter);
This creates a modern, Material-styled drop-down with a hint.
Spinner with Objects
For complex data (custom objects), override toString() or use a custom adapter:
public class Country {
private String code;
private String name;
@Override
public String toString() {
return name; // Spinner shows name
}
}
Use a custom ArrayAdapter<Country> and populate accordingly.
Handling Default Prompts
To avoid auto-select on start, insert a prompt like:
adapter.insert("Choose an option", 0);
spinner.setSelection(0);
Check selection:
if (position == 0) {
// Prompt — not a real selection
}
This improves UX for required fields.
Best Practices (Senior Engineering Insight)
From real-world Android development experience:
- Use string resources for static lists (localization)
- Use custom adapters for objects or enhanced UI
- Keep the prompt at position zero and handle it explicitly
- Avoid long lists directly in Spinner — use searchable dialogs for large data sets
These practices produce maintainable and user-friendly forms.
Common Issues and Fixes
Spinner does not show selection:
✔ Ensure proper adapter is set before listener
Nothing happens on selection:
✔ Check that setOnItemSelectedListener() is set after setAdapter()
Large lists degrade UX:
✔ Use searchable dialogs or AutoCompleteTextView
Summary
A Spinner in Android is a flexible drop-down component ideal for single-choice forms. By setting up an adapter, handling selection events, and using string resources or custom objects, you can deliver intuitive selection interfaces that fit modern app design.
This pattern scales from simple country pickers to complex object selections with clear UI feedback.


