Configuring Spinner Controls

Most of the configuration for a Spinner control must be handled programmatically. As with a ListView control, a Spinner control uses a data adapter to map its contents from a dataset to each view displayed in the control. To load a Spinner control with data, you follow these steps:

1. Retrieve the Spinner control from the layout.

2. Configure a data adapter to map the data to the control.

3. Call the setAdapter() method of the Spinner control.

To load the Spinner control from the layout, you use the familiar findViewById() method:

final Spinner spinner = (Spinner) findViewById(;

Next, you configure your data adapter. A Spinner control displays its data differently when it's closed than when it's open. Therefore, you need to provide layout templates for both display states. Fortunately, the Android platform includes several special layout resources to help create Spinner controls that contain text. Specifically, you can use the layout resource named android.R.layout.simple_spinner_item to create the appropriate view for each item in a default Spinner control. You can use the android.R.layout.simple_spinner_dropdown_item layout resource as the drop-down view resource template.

Using these handy built-in layout templates, you can load your String Array resource called genders into an ArrayAdapter control by using the createFromResource() method:

ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this} R.array.genders, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(


Finally, you call the setAdapter() method of the Spinner control to bind the data to the control:


Was this article helpful?

0 0

Post a comment