How to Use Spinners with Arrays in Android Development Environment

Written by:  • Edited by: Simon Hill
Updated Jul 5, 2011
• Related Guides: Android | HTML

A Spinner is a UI element similar to the “Select” statement in HTML: A list of elements to choose from. Let's work a bit with it.

Last week I was working in an Android application and I needed to create something like a “list” of options, to click and choose. The easiest way to create this kind of element, is using a Spinner. Let's do it with an example.

Imagine we are working in an application that asks the user for personal information -- Name, Age, Occupation...and “Favorite color” (yes, I know, it sounds stupid, but it's just to create an easy example). Well, we have a list of colors and the user has to choose one. How do we create that list of colors? How do we “attach” it to the spinner? In a spinner we can attach more than Arrays, we can attach “Cursors” (from the Database), but this will be explained in our next article.

First let me introduce how to work with arrays in Android.

Array definition in Android

We have two ways to create an Array in Android (Array->List of elements). One of them is create as we did in a pure Java code:

String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};

And the “Android-way” is creating the Array in the “Resources” folder.

We need to create a file called “arrays.xml” in the /res/values/ folder. Here we will write an XML with the following structure.

<resources>

<string-array name="colors">

<item>Red</item>

<item>Blue</item>

<item>White</item>

<item>Yellow</item>

<item>Black</item>

<item>Green</item>

<item>Purple</item>

<item>Orange</item>

<item>Grey</item>

</string-array>

</resources>

We are going to use the “Android-way”, It's the cleanest way in my opinion. Here's a look at how to add this array to the Spinner.

Spinner

In Android, as I have repeated so many times, there are two ways of creating User Interfaces: writing it in Java code, or in the other hand, using the XML resources. I always use XML as it's the cleanest and most organized method (we can easily create a 3-layers application). So, let's create our Spinner in the XML code.

We go inside the XML file we want to add the spinner in, and we put something like this...

…. <XML-Code> (Layouts, Widgets, Whatever)

<Spinner

android:id="@+id/myspinner"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:drawSelectorOnTop="true”

android:prompt="Favorite Color" />

…. </XML-Code> (Layouts, Widgets, Whatever)

If we have worked a bit with Android, some of these attributes are well known, attributes like “android:id”, “android:layout_width”... but there is at least one that is new for us:

android:prompt and maybe android:drawSelectorOnTop

android:prompt is just the title of the list of elements. It is a String and can be something like “Chose your color” or “Favorite Color” as I have written above.

android:drawSelectorOnTop in a “true” value, is just indicating that we are going to draw the selector on the top of the view. What is the meaning of this? Maybe playing with this value we can see the effects on the android screen. It's your homework...and mine!

Well once we have defined the Spinner in XML, let's work a bit in Java code:

Spinner hubSpinner = (Spinner) findViewById(R.id.myspinner);

We create a Spinner Class, using the view we created some lines above.

ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors , android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

We create an ArrayAdapter to wrap our xml array (Array of colours) and we put some pre-defined styles.

hubSpinner.setAdapter(adapter);

And we set the adapter to the spinner.

Thats all! We have created our first spinner!! Any question or suggestions are welcome!


Comments

Showing all 19 comments
 
Guest Dec 31, 2011 8:26 AM
RE: How to Use Spinners with Arrays in Android Development Environment
How would I take a Spinner that already had 10 items in it, and add 1, so it had 11?<br>We have tried every possible idea.<br>Nothing works.<br>
Jbeerdev Oct 20, 2011 12:18 PM
RE: How to Use Spinners with Arrays in Android Development Environment
Hi Nasrin <br><br>You can create your own adapters using "BaseAdapter". Then, you can fill that adapter with whatever you want. <br><br>Here is how to use the adapter with a list, but with a spinner is similar.<br>http://www.brighthub.com/mobil...
nasrin Oct 18, 2011 8:05 PM
RE: How to Use Spinners with Arrays in Android Development Environment
Hi Jbeer,<br> I am facing a problem in spinner .I want to show some list in<br>spinner . for ex: contacts from the contactList. How can I ...<br>Please help<br><br>thank you
vicky Apr 17, 2011 11:11 PM
RE: How to Use Spinners with Arrays in Android Development Environment
Thank you. I have been trying find out how to use a Spinner for a long while now. Finally, your article explains everything perfectly.
aditya Nov 3, 2010 3:28 AM
Add elements dynamicallly
Jbeer..can you tell me how to add elements to string-array dynamically.I want to populate hshtable into string-array and want to show it as dropdown menu.
Bill Oct 30, 2010 10:31 AM
Configuring an Android Spinner control to use an array of java objects
You can find an article that demonstrates populating a spinner control with an array of java objects that can contain much more than the displayed text at http://www.katr.com/article_android_spinner01.php. It shows how to create and populate the control, then how to extract the object in the onItemSelected event.
AT Oct 26, 2010 7:17 PM
its working fine
Thank you, i try that code and it workds fine...
Anonymous Jun 29, 2010 11:25 PM
I get an error from the above code
Question 1:

This line:
ArrayAdapter adapter
= ArrayAdapter.createFromResource( this, R.array.colors , android.R.layout.simple_spinner_item);

Error
ArrayAdapter is a raw type. References to generic type ArrayAdapter<T> should be parameterized.

I read in some other sites that adding <String> or <CharSequence> before the adapter should fix it...is that right?

Question 2:
When I try and run this in an app, when I click on the spinner I get a blank box? Am I misunderstanding what a spinner is?
mala May 1, 2010 2:24 AM
How to access yje other information of the spinner content
Hi Jbeer,
I am facing a problem in spinner .I showed some list in spinner . for ex: contacts from the contactList. I am able to show the selected Item in the spinner..As spinner shows only the name/phone nos.. Now I want to show the others including address details of the corresponding selected contact in some other widget.. How can I ...
Please help

thank you
Jbeerdev Feb 22, 2010 6:19 AM
RE: How to Use Spinners with Arrays in Android Development Environment
Hi!

You can create a SpinnerAdapter, and there, inflate an XML where you can add whatever you want.
NKJ Feb 16, 2010 4:33 AM
adding list view in to Spinner
Hi,

can i add a listview in to spinner,means when we press on the spinner then a list view will come as its child
Jbeerdev Feb 16, 2010 2:38 AM
RE: How to Use Spinners with Arrays in Android Development Environment
Hi Carls!

but, Do you want to show the listview in the spinner pop-up or is just in the screen and when a element is selected the listview appears?. You can do the following:

assing to the spiner a "setOnItemSelectedListener", and inside this listener, you can do whatever you want (show a listview, fill a listview with info get from the spinner...). This is the event that triggers when one item is clicked in the spinner.
charls Feb 16, 2010 2:29 AM
adding list view in to spinner
Hi Jbeer,
Can i add a listview in to spinner.what my concern is when clicking on the spinner one list view will have to come .

Thank you,
charls
Jbeerdev Feb 4, 2010 3:06 AM
RE: How to Use Spinners with Arrays in Android Development Environment
Raja.

You can try this: After adding the ArrayAdapter to the spinner, you can add elements to that arrayAdapter object using the "add" method. In other hand, you can use a "CursorAdapter" to retrieve the elements in a DataBase and place them in the spinner

Ting

Ummm, maybe you should check if there is any event that triggers when the spinner is clicked... I think the onItemClickListener doesn't work....

check it in the Spinner reference page:

http://developer.android.com/reference/android/widget/Spinner.html
Raja Feb 3, 2010 6:40 AM
Help
is it possible to add an item to the array from code?
ting Jan 29, 2010 2:51 AM
Work with Spinner
Hi Jbeer,

Now i having problem with spinner. Let say I have 3 items in the spinner list, and what i wish to do is when i click on the item, it will go to new activity. May I seek advice?

Thanks.
Aarya2 Nov 16, 2009 8:05 AM
Its working fine
it helped me alot thanks jbeer
Jbeerdev Sep 14, 2009 2:21 AM
Flash support for HTC Magic
Hi Big Eyes.

The Flash Support is, lets say, "internal" in the operative system. As far as I know, the 1.6 version of the Android O.S is going to support Flash. So, you just have to way a bit :).
Big Eyes Sep 2, 2009 12:33 PM
Flash support for HTC Magic
Hi Jbeer,

I'm a HTC Magic User from Singapore. I understand that there are no Flash support in the web browser. Is there any way to download it in HTC magic? If yes, what are the steps?

Thank you,
Big Eyes
 
blog comments powered by Disqus
Email to a friend