Android Spinner Dropdown Example

This is a tutorial about simple spinner which allows to select an item from a drop down menu. I am displaying static data in spinner and after selecting an item from spinner a toast message will be shown.

Download Code

Creating new Project

1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
2. Open strings.xml file under resources folder and add following items. Add Spinner dialog tile in string resources.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AndroidSpinnerExample</string>
    <string name="spinner_title">Select Category</string>
</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:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <!-- Text Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Category:"
        android:layout_marginBottom="5dp"
    />

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

4. Open your main activity and extend it from OnItemSelectedListener

public class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{

5. After extending your activity from OnItemSelectedListener write the following code. In the following code i am creating a simple list with list of item needed to show in spinner. And finally i am attaching those items to spinner.

package com.androidhive.spinner;

import java.util.ArrayList;
import java.util.List;

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

public class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List&lt;String&gt; categories = new ArrayList&lt;String&gt;();
        categories.add("Automobile");
        categories.add("Business Services");
        categories.add("Computers");
        categories.add("Education");
        categories.add("Personal");
        categories.add("Travel");

        // Creating adapter for spinner
		ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, categories);

		// Drop down layout style - list view with radio button
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		// attaching data adapter to spinner
		spinner.setAdapter(dataAdapter);
    }

    @Override
	public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
		// On selecting a spinner item
		String item = parent.getItemAtPosition(position).toString();

		// Showing selected spinner item
		Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

	}

	public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
		// TODO Auto-generated method stub

	}

}

6. Run your project.

android_spinner

android spinner dropdown
This image is for thumbnail purpose
android spinner dropdown
Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.