Android Spinner Example

Spinner is a useful view which provides drop-down list for selecting items. The ListView displays a long list of items in an activity, but sometimes we need to display some other views with list, in such cases spinner is useful because it does not require additional space for a full screen like ListView. In this tutorial, I will create a simple Spinner view that dispays a list of days. When one is selected, a toast message will display the selected item.

1.Create a new project and fill the required details:

    Project Name : AndroidSpinnerDemo
    Package : com.sunil.spinnerdemo
    Main Activity :
    Build Target : Android 2.2 

2. Open strings.xml file under res/values/ folder and add following code:

<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name">AndroidSpinnerDemo</string>
    <string name="title_activity_spinner">SpinnerActivity</string>
    <string name="spinner_title">Select Day</string>

3. Open the res/layout/main.xml file and insert the following:

<LinearLayout xmlns:android=""
    android:orientation="vertical" >
      android:text="Select Day:"/>

      android:drawSelectorOnTop = "true"/>

4. Now we need a string resources file to store all list items. So create an XML file under res/values/  folder and name it as list_data.xml and add the following code:

<?xml version="1.0" encoding="utf-8"?>
        <string-array name="day">
            <item >Sunday</item>
            <item >Monday</item>
            <item >Tuesday</item>
            <item >Wednesday</item>
            <item >Thursday</item>
            <item >Friday</item>
            <item >Saturday</item>

5. Now open the file and insert the following code:

package com.sunil.spinnerdemo;

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

public class SpinnerActivity extends Activity {
    String[] day;
    public void onCreate(Bundle savedInstanceState) {
        Spinner spinner = (Spinner) findViewById(;
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this,, android.R.layout.simple_spinner_item);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                int index = arg0.getSelectedItemPosition();
               // storing string resources into Array
               day = getResources().getStringArray(;
                Toast.makeText(getBaseContext(), "You have selected : " +day[index],

            public void onNothingSelected(AdapterView<?> arg0) {
                // do nothing


6. Run your project. (See the screenshots below)

Android Spinner Promptspinner example

That's it ! You can also download the source code of this example.

Download Code

1 Comment:

Alma Dalton said...

Good to see the example of spinner.Spinner is a useful perspective which provides drop-down record for choosing products.i always prefer to see the example than the lengthy content.

Post a Comment

Recent Posts

Site Stats

Our Apps

Get it on Google Play

©2012-2015 AndroidAspect - All rights reserved | Powered by Blogger

Creative Commons License