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 :  SpinnerActivity.java
    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"?>
<resources>
    <string name="app_name">AndroidSpinnerDemo</string>
    <string name="title_activity_spinner">SpinnerActivity</string>
    <string name="spinner_title">Select Day</string>
</resources>

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:orientation="vertical" >
   
    <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Select Day:"/>

    <Spinner
      android:id="@+id/spinner"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:prompt="@string/spinner_title"
      android:drawSelectorOnTop = "true"/>
   
   
</LinearLayout>

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"?>
    <resources>
        <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>
        </string-array>
    </resources>

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

package com.sunil.spinnerdemo;

import android.app.Activity;
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;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
       
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.day, android.R.layout.simple_spinner_item);
       
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
       
        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(R.array.day);
                   
                Toast.makeText(getBaseContext(), "You have selected : " +day[index],
                        Toast.LENGTH_SHORT).show();
               
            }

            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-2014 AndroidAspect - All rights reserved | Powered by Blogger

Creative Commons License