Spinner sample program in Android
By: Ashley
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>
Archived Comments
1. Hi my friend! I want to say that this article is
awesome, great written and include almost a
View Tutorial By: milanuncios motos de agua tenerife at 2017-06-28 05:07:41
2. I am working with spinner how to use 3 sppiners
Baseing one after the other can u plz send
View Tutorial By: BHARGAV at 2016-01-24 15:20:56
3. i have json data like coffeename and coffee prize and i have binded it to spinner but when i touch t
View Tutorial By: Paramatma sharan upadhyay at 2015-09-21 12:14:31
4. Hi, thi's sursh laveti, actually I'm getting a problem with adding a huge no. of cities into spinner
View Tutorial By: suresh l at 2015-06-24 07:53:19
5. nice examples,but i have one problem in my project I want to switch from one activity to another by
View Tutorial By: swati at 2015-04-30 10:48:13
6. hi...i have done a project based on spinners..the probem is when im clicking on spinner it is not di
View Tutorial By: Nagini at 2015-04-11 05:01:43
7. Muy buen ejemplo, aunque como idea general podrÃamos agregar:
spin.setSel
View Tutorial By: Ligator at 2013-07-26 05:57:47
8. 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
9. Very Nice tutorial you can also check this one <a href="http://pavanhd.blogspot.in/2013/04/a
View Tutorial By: pavan at 2013-05-06 12:40:28
10. hi,
i use spinner concept in my project but i will give the spinner item from the sqlite data
View Tutorial By: lavanya at 2013-03-01 06:25:01
11. Thank you....
View Tutorial By: Prakash at 2013-02-13 08:35:12
12. Thanks so much, your post was quit usefull
View Tutorial By: Patrick Chukwuma at 2013-01-10 16:30:38
13. 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
14. Thanks for this example. It helps me a lot.
View Tutorial By: Heniu at 2012-06-29 11:58:01
15. Thank you.. Its working..
View Tutorial By: koti at 2012-06-11 19:53:21
16. hi, the code is fine. i want this array to be declared in an file and it should display the the res
View Tutorial By: deepi at 2012-04-26 06:08:36
17. hi, the code is fine. i want this array to be declared in an file and it should display the the res
View Tutorial By: deepi at 2012-04-26 06:07:13
18. hi, good afternoon
i don,t know how to use spinner class can u give a simple example and wt i
View Tutorial By: Hemanshu at 2012-01-17 06:49:10
19. hi,good afternoon i have one problem with spinners.I am doing my project based upon spinners.My app
View Tutorial By: veera reddy at 2012-01-13 06:55:24
20. Thank you for your simple example which will help me in my application
View Tutorial By: eekld at 2012-01-04 15:46:48
21. the spinner example is very nice...
View Tutorial By: narendra at 2011-11-23 11:16:47
22. i have a problem .
i am getting an error called ib cannot be resolved or its not a field ,sel
View Tutorial By: chaitra at 2011-11-16 07:28:29
23. Hi, I have one problem in spinner items. I want to use the contact list of phone in items of spinner
View Tutorial By: soodi at 2011-10-18 18:15:58
24. Thank you, for simple and useful example.
View Tutorial By: Dmitriy at 2011-09-04 20:53:53
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Solution to error: unable to open connection to server due to security error
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial
Intent in Android to call one activity from another activity.
Progress bar and downloading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
Reading a file sample program in Android
Date and Time sample program in Android
Gallery sample program in Android
GridView sample program in Android
Spinner sample program in Android