This article simply shows how you can implement a Spinner widget in your Android app. The code provided explains straightforward two different methods of creating the Spinners and for allowing multiple Spinners in the same Activity.

In this post, I show you the minimum code required to implement the Spinner. In the attached full source code, there are more advanced features included and all the code is properly placed for you.

 

Creating the Spinner:

The first method shows how you can have all of your Spinner code in the same file. The second method shows the String resources in res/values/strings.xml.

Method 1:

Line 1 should be one of the first things that appear at the start of the Activity. Line 3-6 goes in the onCreate() method.

public static final CharSequence[] DAYS_OPTIONS  = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence> (this, android.R.layout.simple_spinner_item, DAYS_OPTIONS);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner1.setAdapter(adapter); // Apply the adapter to the spinner

Method 2:

<resources>
        <string-array name="planets_array">
            <item>Mercury</item>
            <item>Venus</item>
            <item>Earth</item>
            <item>Mars</item>
            <item>Jupiter</item>
            <item>Saturn</item>
            <item>Uranus</item>
            <item>Neptune</item>
        </string-array>
    </resources>
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
    spinner2.setAdapter(adapter2); // Apply the adapter to the spinner

 

Receiving Callbacks from Multiple Spinners:

Either the activity can implement OnItemSeletectedListener (Method 1) or an anonymous class can be created individually for each Spinner (Method 2). The following snippets of code show what is necessary to add in addition to the above code.

Method 1:

public class MainActivity extends Activity implements OnItemSelectedListener {
    protected void onCreate() { // Contents removed from post for brevity
        spinner1.setOnItemSelectedListener(this);
    }

    /** Spinner Callbacks */
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        /* Use the following switch-statement if you want to keep all spinner actions/callbacks together */
        /* The same can be done to the onNothingSelected callback */
        switch(parent.getId()) {
        case R.id.spinner1:
            // Do stuff for spinner1
            showToast("Spinner1: position=" + position + ", Value= " + DAYS_OPTIONS[position]);
            break;
        // case R.id.spinner2:
        //     // Do stuff for spinner2
        //     showToast("In switch-statement for spinner2. Value=" + parent.getItemAtPosition(position));
        //     break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        Toast.makeText(this, "You selected nothing", Toast.LENGTH_LONG).show();
    }
}

Method 2:

spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        showToast("Spinner2: position=" + position + ", id= " + id + ", value=" + parent.getItemAtPosition(position));
} public void onNothingSelected(AdapterView<?> parent) { showToast("Spinner2: unselected"); } });

 

Bonus Code Snippet:

The following function makes it quicker and easier to create Toasts.

void showToast(CharSequence msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

 

Full Source Code:

There more even more snippets of code included in the attached file.  Comments are included and the code sample runs without any editing required.

http://simplyadvanced.net/apps/android/ExampleSpinner.zip

This post and associated code is completely free, with no advertisements, locked features, or licenses to worry about. If you liked this article, then a small donation would be appreciated. I work hard to sift through all the code sample noise to produce Simply Advanced samples that allow developers to quickly grab-n-go so that you don’t have to worry about the Android APIs. Feel free to email me with suggestions on how to improve on these code samples.

All donations received will be put into use creating more Simply Advanced code samples and improving this website.

References:

[1] Android Spinner Guide

[2] ArrayAdapter