Spinners in Android :

Spinners provide a fast way to choose one value from a group (Items) . Within the default state, a spinner shows its currently selected value (Item). Touching the spinner displays a dropdown menu with all other available values (items) , from which the user can choose a new one.

In this example you will using the ListView to display a list of items, you’ll also use the Spinner view. The Spinner behaves like the drop-down listbox that’s popular within the Windows OS. This example demonstrates the way to use the Spinner.

android spinner dropdown example

 

For this Android Spinner Dropdown example, you will use the Spinner view to display a list of Cities  names.

Note : This example (Project) is developed in Eclipse Version Kepler,tested on Android 4.4.2 (KitKat Version) and SDK build-tools 22.6.3

Solution Stuff :

1. Open “res/layout/activity_main.xml” file and put this xml code it into file:

File : res/layout/activity_main.xml

To use the Spinner, add the <Spinner> element to the UI, such as the activity_main.xml file:

3. Open “MainActivity.java” file and add following JAVA code.

File : src/package-name/MainActivity.java

In the activity, obtain a reference to the Spinner in your UI and assign it to an ArrayAdapter object:

In this example, the ArrayAdapter object loads a list of values from a String array:

Then assign the ArrayAdapter object to the Spinner view:

Te setOnItemSelectedListener() method to assign an instance of the OnItemSelectedListen class so that you can handle the event when the user selects an item in the Spinner:

Output of  Android Spinner Dropdown example in emulator :

android spinner dropdown example

Download Android Spinner Dropdown example Source code :

 

  1. Hi! I’ve been trying to add spinner to my UI but the list won’t display when I tap/click the spinner. 🙁 The same goes for every sample code I found on the net. Please help?

Leave a Reply