ItemSelectedListener for Android Spinner Widget – Example

November 17, 2012

In this article, we will create an Android application with a spinner widget containing a list of countries. On selecting a country from the list, the selected country will be displayed in a TextView widget.

This application is developed in Eclipse (4.2.1) with ADT plugin (20.0.3) and Android SDK (R20.0.3).

1. Create a new Android application project namely “SpinnerWidgetItemSelectedListener”

Create new Android Application project namely "SpinnerWidgetItemSeletedListener"

Figure 1 : Create new Android Application project namely "SpinnerWidgetItemSeletedListener"

2. Design application launcher icon

Designer application launcher icon

Figure 2 : Designer application launcher icon

3. Create a blank activity for define the class MainActivity

Create a blank activity

Figure 3 : Create a blank activity

4. Enter MainActivity details

Enter MainActivity Details

Figure 4 : Enter MainActivity Details

5. Delete Android’s support library from this project, if exists

By default Eclipse ( 4.2.1) adds Android Support Library to  Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”

6. Update the file res/values/strings.xml

    <string name="app_name">SpinnerWidgetItemSelectedListener</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">ItemSelectedListener for Spinner</string>
    <string name="str_spr_countries">Countries</string>

7. Update the file res/values/styles.xml

    <style name="AppTheme" parent="android:Theme" />

8. Update the file res/values-v11/styles.xml

    <style name="AppTheme" parent="android:Theme" />

9. Update the file res/values-v14/styles.xml

    <style name="AppTheme" parent="android:Theme.Holo" />

10. Update the layout of the MainActivity in the file res/layout/activity_main.xml

<RelativeLayout xmlns:android=""
    android:layout_height="match_parent" >

        android:padding="0dp" />

        tools:context=".MainActivity" />


11. Update the MainActivity in the file src/in/wptrafficanalyzer/spinnerwidgetitemselectedlistener/

package in.wptrafficanalyzer.spinnerwidgetitemselectedlistener;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView mTvCountry;
    Spinner mSprCountries;

    // An array containing list of Country Names
    String[] countries = new String[] {
        "Sri Lanka",
        "North Korea",
        "South Korea",

    public void onCreate(Bundle savedInstanceState) {

        // Getting a reference to Spinner Widget containing list of countries
        mSprCountries = (Spinner) findViewById(;

        // Getting a referece to TextView Widget, which displays the selected
        // country from the spinner widget
        mTvCountry = (TextView) findViewById(;

        // Array adapter to set data in Spinner Widget
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, countries);

        // Setting the array adapter containing country list to the spinner widget

        OnItemSelectedListener countrySelectedListener = new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> spinner, View container,
                    int position, long id) {

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

        // Setting ItemClick Handler for Spinner Widget

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

12. Screenshots of the application in execution

Selecting an item from the Android's Spinner Dropdown list

Figure 5 : Selecting an item from the Android's Spinner Dropdown list


The selected item is displayed in the TextView

Figure 6 : The selected item is displayed in the TextView

13. Download

14. Testing with Android Testing Framework

Only the 50% of the application development is completed now. The remaining 50% is with the testing of the application and it is discussed in the article  titled “Testing ItemSelectedListener for Spinner using Android Testing Framework” .

15. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , ,

One Response to ItemSelectedListener for Android Spinner Widget – Example

  1. Lichade on March 28, 2013 at 5:17 am

    This is not a widget!

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner