Android Spinner (DropDown List) Example

Standard

According to official documentation “Spinners provide a quick way to select one value from a set”. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

Spinner is a widget similar to a drop-down list for selecting items. you can use “android.widget.Spinner” class to render a dropdown box selection list.

In this tutorial, we will create simple android application which demonstrate two ways of assigning value is spinner (Using xml and dynamic / programmatically  ). First we spinner (Country Spinner) will get value from string.xml file and other spinner (Business Type) will get value dynamically .  Below is summary of task which we will cover in this tutorial.

  1. Render a Spinner in XML, and load the selection items via XML file also.
  2. Render another Spinner in XML, and load the selection items via code dynamically.
  3. Attach a listener on Spinner, fire when user select a value in Spinner.

Android Spinner (DropDown List) Example

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2.  Open “res/values/strings.xml” file, define the list of items that will display in Spinner (dropdown list).
           File : res/values/strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Pulse7 Demo</string>
        <string name="business_prompt">Choose a Bussiness Type</string>
        <string name="country_prompt">Choose a Country</string>
        <string-array name="country_arrays">
            <item>India</item>
            <item>United States</item>
            <item>United Kingdom</item>
            <item>France</item>
            <item>Italy</item>
            <item>Singapore</item>
            <item>New Zealand</item>
        </string-array>
    </resources>
  3. Now open your main.xml and design a simple layout with text label and a spinner.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:orientation="vertical" android:layout_width="fill_parent"
    	android:layout_height="fill_parent">
    	<TextView android:layout_width="fill_parent"
    		android:layout_height="wrap_content" android:text="Pulse 7 - Spinner Demo"
    		android:textColor="#fff" android:textSize="20sp" />
    	<TextView android:layout_width="fill_parent"
    		android:layout_height="wrap_content" android:text="Select Country"
    		android:textColor="#fff" android:textSize="20sp" />
    
    	<Spinner android:layout_width="match_parent"
    		android:layout_height="wrap_content" android:id="@+id/spCountries"
    		android:prompt="@string/country_prompt" android:entries="@array/country_arrays"></Spinner>
    	<TextView android:layout_width="fill_parent"
    		android:layout_height="wrap_content" android:text="Select Business Type"
    		android:textColor="#fff" android:textSize="20sp" />
    
    	<Spinner android:layout_width="match_parent"
    		android:layout_height="wrap_content" android:id="@+id/spBussinessType"
    		android:prompt="@string/business_prompt"></Spinner>
    
    </LinearLayout>
  4. Now open your Activity Class and put following code.
    package com.vrs.pulse7;
    
    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 Pulse7DemoActivity extends Activity {
    
    	// Wigets - GUI
    	Spinner spCountries;
    	Spinner spBusinessType;
    
    	// Data Source
    	String businessType[] = { "Automobile", "Food", "Computers", "Education",
    			"Personal", "Travel" };
    
    	// Adapter
    	ArrayAdapter<String> adapterBusinessType;
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    
    		// Initialize Spinners
    
    		spCountries = (Spinner) findViewById(R.id.spCountries);
    		spBusinessType = (Spinner) findViewById(R.id.spBussinessType);
    
    		// Initialize and set Adapter
    		adapterBusinessType = new ArrayAdapter<String>(this,
    				android.R.layout.simple_spinner_item, businessType);
    		spBusinessType.setAdapter(adapterBusinessType);
    
    		// Country Item Selected Listener
    		spCountries.setOnItemSelectedListener(new OnItemSelectedListener() {
    
    			@Override
    			public void onItemSelected(AdapterView<?> adapter, View v,
    					int position, long id) {
    				// On selecting a spinner item
    				String item = adapter.getItemAtPosition(position).toString();
    
    				// Showing selected spinner item
    				Toast.makeText(getApplicationContext(),
    						"Selected Country : " + item, Toast.LENGTH_LONG).show();
    			}
    
    			@Override
    			public void onNothingSelected(AdapterView<?> arg0) {
    				// TODO Auto-generated method stub
    
    			}
    		});
    		// Business Type Item Selected Listener
    		spBusinessType.setOnItemSelectedListener(new OnItemSelectedListener() {
    
    			@Override
    			public void onItemSelected(AdapterView<?> adapter, View v,
    					int position, long id) {
    				// On selecting a spinner item
    				String item = adapter.getItemAtPosition(position).toString();
    
    				// Showing selected spinner item
    				Toast.makeText(getApplicationContext(),
    						"Bussiness Type : " + item, Toast.LENGTH_LONG).show();
    			}
    
    			@Override
    			public void onNothingSelected(AdapterView<?> arg0) {
    				// TODO Auto-generated method stub
    
    			}
    		});
    	}
    }
  5.  Now Execute the application
    Android Spinner (DropDown List) Example

    Application Started

    Android Spinner (DropDown List) Example

    Click on Country Spinner

    Android Spinner (DropDown List) Example

    Select Spinner Option, It will display selected value

     

Points to Remember

android:entries  property is used for assigning data source value to Spinner Control.
android:prompt  property is used for setting Title in spinner list.
setOnItemSelectedListener method is used for listening item selection action in Spinner Control

I hope you like this article. Share your views to to improve content.

 

Vimal Rughani

About vimal rughani

Working as Creative Product Manager at Virtual Reality Systems, Rajkot, India

  • divya

    i would like to know how to insert the values of radio group and spinner into SQLite

    • vimal rughani

      Hi Diya,
      To get value of Spinner you can use onItemSelected method of OnItemSelectedListener Listener. Once you have that value, you can store that value in SQLite database using its insert method and contentvalues class. Please check this link for more details : http://pulse7.net/android/sqlite-database-android/ which show how to insert value in SQLite DB.

      Please let me know if you have any difficulty.