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
Subscribe
Notify of
guest
61 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jhonny Makis
Jhonny Makis
8 years ago

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

mangesh kaduskar
mangesh kaduskar
8 years ago

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
alfian
8 years ago

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
Navdeep Agrawal
8 years ago

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
yoominho
8 years ago

thank you

Gokul
Gokul
8 years ago

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

nilesh
nilesh
7 years ago
Reply to  Gokul
george
george
7 years ago

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 [email protected] . Thanks ang
good luck for all your projects..

kapil goyal
kapil goyal
6 years ago
Reply to  george

hi all…

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

Name
Name
7 years ago

super

hellboy
hellboy
7 years ago

one of the best site
u hav all d resourses

divya
divya
7 years ago

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
anwar sathath mohamed jaffarul
7 years ago

good material, it is working fine

Guest
Guest
7 years ago

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
Somnath
6 years ago

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
Sumanta B
6 years ago
Reply to  Somnath

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

Rana Saqib Manj
Rana Saqib Manj
6 years ago

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

Bernadeth Cacao
Bernadeth Cacao
6 years ago

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
Pramod HN
6 years ago

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
hackerkernel
6 years ago
dhara
dhara
5 years ago

It Doesn’t display Radio Buttton

tarun sharma
tarun sharma
5 years ago
Reply to  dhara

hi Dhara

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

tarun sharma
tarun sharma
5 years ago

Hi Ravi,

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

Mario German Agudelo
Mario German Agudelo
5 years ago

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
panchami Mohan
5 years ago

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

Divya Munjal
Divya Munjal
5 years ago

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

Abhishek Malviya
Abhishek Malviya
4 years ago
Reply to  Divya Munjal

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

Shubham Sharma
Shubham Sharma
4 years ago

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
Talal AboCarlos Oman
4 years ago

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

Farrukh Saleem
Farrukh Saleem
4 years ago

when i run this code then lot of errors in it.
i am using android studio.
see errors in image file
comment image

Dilshad Khan
Dilshad Khan
4 years ago
Reply to  Farrukh Saleem

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);

Farrukh Saleem
Farrukh Saleem
4 years ago
Reply to  Dilshad Khan

thanks bro

Ray Vaniya
Ray Vaniya
4 years ago

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

Anoop Varghese
Anoop Varghese
3 years ago

How to display large string when an item in select catagory is selected?Suppose if someone selects automobile i need to show some description about automobile as text.Like that for education another text.

hasim shaikh
hasim shaikh
3 years ago

i am getting this error:
Attempt to invoke virtual method ‘void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)’ on a null object reference

i have implemented OnItemSelectedListener also

Niyanta
Niyanta
3 years ago

I have applied this method but it does not work.Can anyone tell me what can i do?????comment image comment image

Padmaja Rani Bodsakurthi
Padmaja Rani Bodsakurthi
3 years ago

can u please post the searchable spinner source code

Malini
Malini
3 years ago

Hi ravi,
How to post edit text and spinner data(get spinner data from service)to server.I need both get and post in same file.
Thanks in advance.

Ravi Tamada
3 years ago
Reply to  Malini

Could you explain your questions a bit?

Malini
Malini
3 years ago
Reply to  Ravi Tamada

I want to post simple form data to server using all controls like edittext,spinner and image etc.for that i need to get spinner’s data and bind to form

wenjuan
wenjuan
2 years ago

i used simple_spinner_dropdown_item layout, but did not get same nice drop down view as you.

amit
amit
9 years ago

nice

wArLeY996
wArLeY996
9 years ago

Thnkz Ravi, all examples are really useful.

Dkumar Okaya
Dkumar Okaya
9 years ago

good

serhat öden
serhat öden
9 years ago

thx

Krishnaveni
Krishnaveni
9 years ago

easy steps…..nice

Arun Yadav
Arun Yadav
9 years ago

It is so nice and thanks for such a brilliant support to the individuals who are very new to android technology like me… the best thing is that it’s explanation..

Yasmine
Yasmine
9 years ago

Thanks for you example, but I have a question ;
How Can I use this spinner list to open activities.??

Imrant24
Imrant24
9 years ago
Reply to  Yasmine

how to get values in editext from sqlitedatabase through id
mail [email protected]

Imrant24
Imrant24
9 years ago
Reply to  Imrant24

mail code to [email protected]

Abc
Abc
8 years ago
Reply to  Imrant24

…..

mithilesh
mithilesh
9 years ago

Nice and easy tutorial.
Can you share a tutorial on how to customize the spinner and the dropdown menu.

Sandeep Verma
9 years ago

Thanks Ravi, Very useful for my upcoming game project 🙂

Kimfai109
Kimfai109
9 years ago

hi Ravi, thanks for your tutorial, i am new to android development, i want to ask how can i dynamic display approriate UI elements on a activity that are about my received data returned by JSON because im getting all relevant data from MYSQL
thanks for you quick replay

Kedar Parmar
Kedar Parmar
9 years ago

Thanx very much its very useful to me.

Asrinivasulua
Asrinivasulua
9 years ago

List<String> categories = new ArrayList<String>(); put in place of this

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

it will work

Tuan
Tuan
9 years ago

Simple to understand, clear code, thank you so much

Samadhanmedge
Samadhanmedge
9 years ago

nice tutorial, But i want to give key-value pair to spinner item, how to give it

Tanu
Tanu
9 years ago

can u please write some tutorials for animation at activity start up ar calling intent etc.

Harish
Harish
8 years ago

hi

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

devappandroid
devappandroid
4 years ago

Thanks for helping this. It is greate source code for the spinner app.

I also share some new code for asynctask example.

http://devappandroid.com/android-beginner/android-asynctask-example-source-code

61
0
Would love your thoughts, please comment.x
()
x