Spinner sample program in Android

By: Ashley Printer Friendly Format    


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>


Most Viewed Articles (in Android )

Latest Articles (in Android)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Thanks so much, your post was quit usefull
View Tutorial          By: Patrick Chukwuma at 2013-01-10 16:30:38

2. Thank you, for simple and useful example.
View Tutorial          By: Dmitriy at 2011-09-04 20:53:53

3. Hi, I have one problem in spinner items. I want to
View Tutorial          By: soodi at 2011-10-18 18:15:58

4. i have a problem .
i am getting an error ca

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

5. the spinner example is very nice...
View Tutorial          By: narendra at 2011-11-23 11:16:47

6. Thank you for your simple example which will help
View Tutorial          By: eekld at 2012-01-04 15:46:48

7. hi,good afternoon i have one problem with spinner
View Tutorial          By: veera reddy at 2012-01-13 06:55:24

8. hi, good afternoon
i don,t know how to use

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

9. hi, the code is fine. i want this array to be decl
View Tutorial          By: deepi at 2012-04-26 06:07:13

10. hi, the code is fine. i want this array to be decl
View Tutorial          By: deepi at 2012-04-26 06:08:36

11. Thank you.. Its working..
View Tutorial          By: koti at 2012-06-11 19:53:21

12. Thanks for this example. It helps me a lot.
View Tutorial          By: Heniu at 2012-06-29 11:58:01

13. I want get data from Mysql into Spinner by using P
View Tutorial          By: Pravin at 2013-01-05 13:36:11

14. Thank you....
View Tutorial          By: Prakash at 2013-02-13 08:35:12

15. hi,
i use spinner concept in my project but

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

16. Very Nice tutorial you can also check this one <
View Tutorial          By: pavan at 2013-05-06 12:40:28

17. I want get data from Mysql into Spinner by using P
View Tutorial          By: Dilsha at 2013-07-06 05:17:58

18. Muy buen ejemplo, aunque como idea general podr&At
View Tutorial          By: Ligator at 2013-07-26 05:57:47

19. hi...i have done a project based on spinners..the
View Tutorial          By: Nagini at 2015-04-11 05:01:43

20. nice examples,but i have one problem in my project
View Tutorial          By: swati at 2015-04-30 10:48:13

21. Hi, thi's sursh laveti, actually I'm getting a pro
View Tutorial          By: suresh l at 2015-06-24 07:53:19

22. i have json data like coffeename and coffee prize
View Tutorial          By: Paramatma sharan upadhyay at 2015-09-21 12:14:31

23. I am working with spinner how to use 3 sppiners <
View Tutorial          By: BHARGAV at 2016-01-24 15:20:56

24. Hi my friend! I want to say that this article is <
View Tutorial          By: milanuncios motos de agua tenerife at 2017-06-28 05:07:41