Spinner sample program in Android

By: Ashley Emailed: 1646 times Printed: 2115 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This sample android program shows you how to use Spinner  in Android. In this program a list is shown as a dropdown box. When you click on the list, the selected item is shown on the text view. You can use this ArrayAdapter widget and the Spinner object together with the  onListItemClick() method to determine the selected index and process accordingly.

The ArrayAdapterDemo2.java file is as follows:

package com.javasamples;
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.TextView;

public class ArrayAdapterDemo2 extends Activity implements
		AdapterView.OnItemSelectedListener {
	TextView selection;
	String[] items = { "this", "is", "a", "really", 
			"really2", "really3",
			"really4", "really5", "silly", "list" };

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		selection = (TextView) findViewById(R.id.selection);

		Spinner spin = (Spinner) findViewById(R.id.spinner);
		spin.setOnItemSelectedListener(this);

		ArrayAdapter aa = new ArrayAdapter(
				this,
				android.R.layout.simple_spinner_item, 
				items);

		aa.setDropDownViewResource(
		   android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(aa);
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		selection.setText(items[position]);
	}

	public void onNothingSelected(AdapterView<?> parent) {
		selection.setText("");
	}
}//class

The output of this program will be as shown in the android emulator below.

The main.xml file in your res/layout folder is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/myLinearLayout"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<TextView
		android:id="@+id/selection"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:background="#ff0033cc"
		android:textSize="14dip"
		android:textStyle="bold">
	</TextView>
	<Spinner
		android:id="@+id/spinner"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
	</Spinner>
</LinearLayout>

Android Home | All Android Tutorials | Latest Android Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(18)


1. View Comment

Thank you, for simple and useful example.

View Tutorial          By: Dmitriy at 2011-09-04 20:53:53
2. View Comment

Hi, I have one problem in spinner items. I want to use the contact list of phone in items of spinner . what do I do?
and , I have some xml files for layout , I want when Iclick in one button in one layout , can go to another layout(xml file).
May send the answers to my Email.thx.


View Tutorial          By: soodi at 2011-10-18 18:15:58
3. View Comment

i have a problem .
i am getting an error called ib cannot be resolved or its not a field ,selection canot be resolved or it is a field .


View Tutorial          By: chaitra at 2011-11-16 07:28:29
4. View Comment

the spinner example is very nice...

View Tutorial          By: narendra at 2011-11-23 11:16:47
5. View Comment

Thank you for your simple example which will help me in my application

View Tutorial          By: eekld at 2012-01-04 15:46:48
6. View Comment

hi,good afternoon i have one problem with spinners.I am doing my project based upon spinners.My app is to find out the cities in the given states.By using two spinners this is should be done..I am done in the first spinner i have added the states in the next spinner corresponding cities are displayed..The Problem is started here when i am selected the state corresponding city will be displayed.In the 1st position i have city in the next intent it will be displayed villages wen i am selected another state that cities same intent will be displayed to other stats...please rectify this problem

View Tutorial          By: veera reddy at 2012-01-13 06:55:24
7. View Comment

hi, good afternoon
i don,t know how to use spinner class can u give a simple example and wt is the use of spinner and where it is used.
please send this answer to above my email id.. thanks.....


View Tutorial          By: Hemanshu at 2012-01-17 06:49:10
8. View Comment

hi, the code is fine. i want this array to be declared in an file and it should display the the result for the same how to use an file and declare this array inside the file.can any one help me ?

View Tutorial          By: deepi at 2012-04-26 06:08:36
9. View Comment

Thank you.. Its working..

View Tutorial          By: koti at 2012-06-11 19:53:21
10. View Comment

I want get data from Mysql into Spinner by using Php and json
Please Help me


View Tutorial          By: Pravin at 2013-01-05 13:36:11
11. View Comment

Thank you....

View Tutorial          By: Prakash at 2013-02-13 08:35:12
12. View Comment

hi,
i use spinner concept in my project but i will give the spinner item from the sqlite database what i do?


View Tutorial          By: lavanya at 2013-03-01 06:25:01
13. View Comment

I want get data from Mysql into Spinner by using Php and json
Please Help me


View Tutorial          By: Dilsha at 2013-07-06 05:17:58
14. View Comment

Muy buen ejemplo, aunque como idea general podríamos agregar:

spin.setSelection(int i);

Donde i es el índice del arreglo "items[]", y establece el valor inicial del spinner.


View Tutorial          By: Ligator at 2013-07-26 05:57:47
15. View Comment

hi...i have done a project based on spinners..the probem is when im clicking on spinner it is not displaying its values..so that my project is not excuting..my project is done using PHP ,my sql,json and android...please help..

View Tutorial          By: Nagini at 2015-04-11 05:01:43
16. View Comment

nice examples,but i have one problem in my project I want to switch from one activity to another by selecting spinner item using intent, how can i deal with this problem ..?

View Tutorial          By: swati at 2015-04-30 10:48:13
17. View Comment

Hi, thi's sursh laveti, actually I'm getting a problem with adding a huge no. of cities into spinner.so can any one please help me how to adding cities to spinner on android studio.

View Tutorial          By: suresh l at 2015-06-24 07:53:19
18. View Comment

i have json data like coffeename and coffee prize and i have binded it to spinner but when i touch the spinner then it is not selecting value but when i am binding hard coded data from the String then it is going to bind and got selected so plz help me to get json data in spinner

View Tutorial          By: Paramatma sharan upadhyay at 2015-09-21 12:14:31

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ashley
Is autodiscover CNAME a must in Office 365
Directory Synchronization tool in Office 365
Configure Outlook Manually for Office 365
Review: Ruggear - The world's most Rugged Phone in Singapore
Solution to error: unable to open connection to server due to security error
Making your Alias email as 'Send As' in Office 365
Steps to Create Shared Mailbox in office 365
xxxx cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
The user's Outlook Live mailbox is full.
Outlook Live couldn't connect to the IMAP server.
The user's mailbox has already been migrated.
An Outlook Live mailbox wasn't found for the user.
Outlook Live couldn't sign in to the user account on the IMAP messaging system.
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial

More Tutorials in Android
Getting Started with Android
Solution to error: unable to open connection to server due to security error
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Android Emulator
Intent in Android to call one activity from another activity.
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Reading a file sample program in Android

More Latest News
Most Viewed Articles (in Android )
Progress bar and downloading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
Spinner sample program in Android
Intent in Android to call one activity from another activity.
ArrayAdapter sample program in Android
Reading a file sample program in Android
Animation sample program in Android
GridView sample program in Android
Android Send SMS and Make a phone call in Android Application
Gallery sample program in Android
Date and Time sample program in Android
Getting Started with Android
Android Preferences - Using Preferences in Android Tutorial
Solution to error: unable to open connection to server due to security error
AlertDialog sample program in Android
Most Emailed Articles (in Android)
Intent in Android to call one activity from another activity.
Getting Started with Android
Error generating R.java from manifest
AlertDialog sample program in Android
Date and Time sample program in Android
Reading a file sample program in Android
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Another Animation sample program in Android
ArrayAdapter sample program in Android
Progress bar and downloading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
Android Emulator
Animation sample program in Android
Spinner sample program in Android