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 Class

Author: Ian Darwin
Published? true
FormatLanguage: WikiFormat

Problem:

You want to offer a drop-down choice item.

Solution:

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

Discussion:

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"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:prompt="@string/context_choice"/>

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"?>
<resources>
        <string name="context_choice">When Reading Taken</string>       
    <string-array name="context_names">
        <item>Breakfast</item>
        <item>Lunch</item>
        <item>Dinner</item>
        <item>Snack</item>
    </string-array>
</resources>

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);   
        adapter.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
        contextChooser.setAdapter(adapter);

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() {

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

			@Override
			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.

No records found.