Group Services: Technology Consulting
phone +91-9999-283-283/9540-283-283
email info@sisoft.in

Android Concept - Adapter View : Spinner


Spinners are drop down list, where by multiple options was shown and user may selected one of those. This is type of adapter control in Android. The adapter controls are data driven controls. We need to understand how to add this control on the screen, how to populate it, how to use the selected data and what are various formatting options


How to Add This Control

This control can be added by pulling Spinner(Sub Item) on the layout screen. This will add following text in XML file:

android:entries="@array/Sky_Colors" />

There is special tag :entries. This tag populates the spinner from array data items.


How to Populate Data Items

Data source may be populated either from Array defined in strings.xml(res/values folder) or array defined in the program.

For defining array in strings.xml, it can be added using add option in Android Resource screen. Then select Integer Array or String Array. Array Name should be mentioned. Then items should be added with suitable value. A sample string will have following entry:

<string-array name="Sky_Colors">
<item >Red</item>
<item >Blue</item>
<item >Green</item>
<item >Silver</item>
This array can be referenced from layout xml file in property :entries.

ArrayAdapter can be created using by strings arrary. For this createFromResource method is used.

ArrayAdapter<CharSequence> ab = ArrayAdapter.createFromResource(getApplicationContext(), R.array.Sky_Colors, android.R.layout.simple_spinner_item);

Arrayadapter may be created using program array. The syntax for this is as follows:

ArrayAdapter<String> aa= new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, names);

The R.layout.simple_spinner_item is the layout resource that defines how the selected choice will appear in the spinner control.

Once the ArrayAdapter is created, it can set as data adapter for the spinner using method setAdapter.



How to Select data items:

When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event and its interface implementation be specified by calling setOnItemSelectedListener()
To define the selection event handler for a spinner, implement the AdapterView.OnItemSelectedListener interface. This interface requires the onItemSelected() and onNothingSelected() callback methods

spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> parent, View v, int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Item selected"+parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Nothing Selected", Toast.LENGTH_LONG).show();



About the Contributor...
Vijay Rastogi
   Mobility Consultant at Sisoft Technologies Indirapuram Ghaziabad