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.
  • Jhonny Makis

    Thanks Ravi!! Very useful!!
    Muito Obrigado, o seu site está me ajudando bastante!

  • mangesh kaduskar

    Spinner spinme=(Spinner)findViewById(R.id.spinme);

    final MyData spin1[] = new MyData[5];

    spin1[0] = new MyData( “Hello”,”World” );

    spin1[1] = new MyData( “Android”,”Here” );

    ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_spinner_item,spin1);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinme.setAdapter(adapter);

    spinme.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override

    public void onItemSelected(AdapterView arg0, View arg1,int arg2, long arg3) {

    // TODO Auto-generated method stub

    int a= spin1[arg2];

    String b = arg0.getItemAtPosition(arg2).toString();

    }

    @Override

    public void onNothingSelected(AdapterView arg0) {

    // TODO Auto-generated method stub

    }

    });

  • alfian

    hi ravi, such a nice tutorial. i have tried your code, but it can’t show prompt message like in the picture. how can i show prompt for spinner?

  • Navdeep Agrawal

    Hay Ravi why are you change design of our page?
    I think previous view was better than current view because readable content should be center in page.

  • yoominho

    thank you

  • Gokul

    how to set background color to dropdown items in android …………..rply it

  • george

    Hi all. I am a novice in developing app and thanks to
    Ravi for all good work that is doing . I’m working at a simple app and I get
    stuck . if you have knowledge of
    building android apps, and make team
    with me please replay to george.celsie@gmail.com . Thanks ang
    good luck for all your projects..

    • kapil goyal

      hi all…

      i am also a novice in app developement.. thanks ravi

  • Name

    super

  • hellboy

    one of the best site
    u hav all d resourses

  • divya

    can anyone guide to view the output in android phone instead of emulator
    i have installed adb driver but i cant able to see my device in devices

  • anwar sathath mohamed jaffarul

    good material, it is working fine

  • Guest

    hi Ravi its not working fine give error at

    List<String> categories = new ArrayList<String>();

    categories.add(“Automobile”);

    categories.add(“Business Services”);

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinStatus.setAdapter(dataAdapter);

  • Somnath

    Hello Ravi,
    Nice Tutorial, However, when we select the same item from the spinner. It does not call the OnItemSelected Method. Any workarounds?

    • Sumanta B

      you have use this line spinner.setOnItemSelectedListener(this);

  • Rana Saqib Manj

    how to add design in spinner not a list ….. how is it possible ??

  • Bernadeth Cacao

    Hello Mr.Rav Tamada. Why I can’t received any email verification on my email. I really wanted to try your tutorial. But I can’t subscribe in this site. Please help me. Thanks. 🙂

  • Pramod HN

    sir, how to access the image button through spinner,, example: when ever we click on particular city on spinner.. the related areas to that city should displayed in image button,,, that is each area should be displayed in individual image button…

  • hackerkernel
  • dhara

    It Doesn’t display Radio Buttton

    • tarun sharma

      hi Dhara

      did you get any solution for this beacause m having the same problem.

  • tarun sharma

    Hi Ravi,

    I coded the this one but it is not displaying the Radio Button.

  • Mario German Agudelo

    Thank you very much for sharing your knowledge with everyone, could you help me I need to place a spinner in a recyclerview, you have some tutorial or link in which to do this process, I appreciate all your help.

  • panchami Mohan

    Hi, is it possible to select multiple items in spinner? Thank you

  • Divya Munjal

    can anyone tell me how to select multiple values from spinner?

    • Abhishek Malviya

      Spinner is mutually exclusive u can select only one item at a time

  • Shubham Sharma

    country ,state and district k liye spinner kese banayenge JSON use kerke volley k saath.agar country per click kere to india select ho and state me india k states aaaye.
    please tell me.

  • Talal AboCarlos Oman

    thank you sir
    and i hope to see three spinner like customer location
    country, state, city
    use php & mysql

  • Farrukh Saleem

    when i run this code then lot of errors in it.
    i am using android studio.
    see errors in image file
    https://uploads.disquscdn.com/images/8ba29d685fb97e0a5f4720e438dc492a1738eb5089bbbafd4862b96f81bf1fc9.jpg

    • Dilshad Khan

      Bro @Farrukh you are using code in Android Studio and basically this tutorial is so old that’s way you got some error in this code so try this code

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

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

      // Spinner Drop down elements
      List categories = new ArrayList();
      categories.add(“Automobile”);
      categories.add(“Business Services”);
      categories.add(“Computers”);
      categories.add(“Education”);
      categories.add(“Personal”);
      categories.add(“Travel”);

      // Creating adapter for spinner
      ArrayAdapter dataAdapter = new ArrayAdapter (getActivity(), 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);

  • Ray Vaniya

    how to achieve same functionality using data binding, can you please provide full functional example with code