Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art
Offering a Drop-Down Chooser via the Spinner ClassBack to Android Cookbook Home | Up to Chapter: Graphical User Interface

Author: Ian Darwin ('idarwin')
In Published Edition? Yes
FormatLanguage: WikiFormat

Offering a Drop-Down Chooser via the Spinner Class


You want to offer a drop-down choice item.


Use a Spinner object; you can pass the list of selections as an Adapter.


Generally known as a 'combo box', the Spinner is the analog of the HTML SELECT or the Swing JComboBox. It provides a drop-down chooser whose values appear to float over the screen when the Spinner is clicked. One item can be selected and the floating version will pop down, displaying the selection in the Spinner.

Like all standard components, the Spinner can be created and customized in XML. In this example - from healthcare practice - the term "Context" is used to indicate the phase of the person's day when a reading was taken - after breakfast, after lunch, etc., so the health care practitioner can understand the value in context of the patient's day. Here is an excerpt from res/layout/main.xml:

<Spinner  android:id="@+id/contextChooser"

Ideally the list of values won't be hard-coded but will come from a Resource file, so as to be internationalizable. Here is a file res/values/contexts.xml containing the XML values for the list of times to choose.

<?xml version="1.0" encoding="utf-8"?>
        <string name="context_choice">When Reading Taken</string>       
    <string-array name="context_names">

To tie the list of Strings to the Spinner at run time, just locate the Spinner and set the values, as shown:

Spinner contextChooser = (Spinner) findViewById(R.id.contextChooser);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.context_names, android.R.layout.simple_spinner_item);   

That is all you need in order for the Spinner to appear, and to allow the user to select items. If you want to know the chosen value right away, you can send an instance of OnItemSelectedListener to the Spinner's setOnItemSelectedListener. This interface has two callback methods, setItemSelected and setNothingSelected. Both are called with the Spinner (but the argument is declared as a ViewAdapter); the former method is also called with two integer arguments, the list position and the identity of the selected item.

        contextChooser.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> spinner, View arg1,
					int pos, long id) {
				Toast.makeText(SpinnerDemoActivity.this, "You selected " + contextChooser.getSelectedItem(), Toast.LENGTH_LONG).show();

			public void onNothingSelected(AdapterView<?> spinner) {
				Toast.makeText(SpinnerDemoActivity.this, "Nothing selected.", Toast.LENGTH_LONG).show();

On the other hand, you may not need the value from the Spinner until the user fills in multiple items and clicks a Button. In this case, you can simply call the Spinner's getSelectedItem() method, which returns the item place in that position by the Adapter. Assuming you placed Strings in the list, you can just call toString() to get back the given String value.

See Also