Android Developer Reference

This is the official Google Android site for developers. Here you will find all the documentation you need to develop rich android applications.

You will most likely visit this site often.

All tutorials and sample code are credited to their respected owners.

Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.


This topic covers the android spinner widget.

Spinners are simple to use, easy to program and very useful. There are several ways to populate a Spinner and they will be touched upon in turn.

The Android Developer's Website focuses on one of these methods and also contains the full reference guide for the Spinner. This topic will cover multiple ways to populate an Android Spinner.

Populating a Spinner from a string array resource is one of the most common and most documented way to populate a spinner. It will allow you to have a preset list of items loaded at creation time. To do this, put that list into your strings.xml file as a string-array, and then pass that array to your spinner using a simple array adapter. An example of the string-array is shown below:

The code above is basic XML code. Create a string-array item, give it a unique name, and fill it with items. Next you need to create an actual spinner widget in your layout file. You can also create the spinner using Java and load the array that way, this technique will be explained later in this topic. The XML method is generally the preferred way, but it is your choice.

Now we will look at how we can add a spinner to our layout file.


Now all need to do is hook up an adapter to load the spinner with our string-array we defined before. An adapter is like an interface between the widget and your data and is required for loading most list type widgets.

The code for our adapter is listed below.

Here we just create an adapter using the createFromResource() method, which takes a context (usually 'this'), a reference to a resource (i.e. our string-array) and a reference to a layout view. Then we also should specify a layout view for the drop down items when someone clicks on the spinner. In this example we are using the built-in android layout views, which work very well for simple spinners, but you can also use your own custom views which allow you to make some customizations to the spinner. Next, we need to attach the adapter to the spinner, the code is shown below.

So here, we create a reference to our spinner using the findViewbyId() method passing it the id of our spinner we defined previously in our XML layout. And then assign our adapter to the spinner using the setAdapter() method of the spinner.

Loading a Spinner from a Database

The Cursor Adapter is specially designed to retreive data from a database. The Cursor Adapter uses a pointer object called a Cursor to retrieve a particular data record and pull the data from a specific column in that data record, then assign that Cursor object to the Adapter.

This procedure is explained below. In this example it is assumed you already have a method that retreives a database record and returns a Cursor object, If you don't have one defined there is a basic one shown below as well.

Now, to load the adapter we can do something like this:

Loading a Spinner Manually

You can also populate a spinner manually by calling the add() method of the adapter. An example of this is shown below.

Now we can use the add() method of the adapter to load our items manually.

Now you can attach the adapter to the spinner using the setAdapter() method of the spinner as shown previously.

Some additional information about spinners and their adapters are given below.