Archive for March, 2012

Spinner in Android

Hope you all have heard about drop down menu. Spinner is a widget in android which behaves in a similar manner.

In this simple tutorial I’ve discussed about how to implement a Spinner in android.

Let’s first look at the XML layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
<Spinner 
 android:id="@+id/spnrmonths"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:prompt="@string/title" 
 />
</LinearLayout>

In the above xml file, android:prompt=”@string/title” attribute of the Spinner is used to display the title when the Spinner is selected. The title string is stored in the strings.xml file in res/values/ folder.

The strings.xml file is shown below:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MySpinnerActivity!</string>
 <string name="app_name">MySpinner</string>
<string-array name="months">
<item >January</item>
 <item >February</item>
 <item >March</item>
 <item >April</item>
 <item >May</item>
 <item >June</item>
 <item >July</item>
 <item >August</item>
 <item >September</item>
 <item >October</item>
 <item >November</item>
 <item >December</item>
    </string-array>
 <string name="title">Choose a Month</string>
</resources>

Here, the <string-array> element named “months” defines the list of strings (here, name of all the months in a year) which will be displayed as a list when we click the Spinner widget.

Now, this data is provided to the Spinner through an ArrayAdapter as coded below.

ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(getApplicationContext(), R.array.months, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Now, we need to create a reference to the widget, Spinner and set the adapter.

final Spinner myspinner=(Spinner)findViewById(R.id.spnrmonths);
myspinner.setAdapter(adapter);

Next, let’s display a Toast when an item in the list displayed is selected.

How is this done? Just have a look at the code below.

myspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
 // TODO Auto-generated method stub
 Toast.makeText(getApplicationContext(), "You selected "+(CharSequence) arg0.getItemAtPosition(arg2), Toast.LENGTH_SHORT).show();
  }
public void onNothingSelected(AdapterView<?> arg0) {
 // TODO Auto-generated method stub

 }
 });

Now, let’s look at the whole code:

package com.deepthi.myspinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MySpinnerActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 final Spinner myspinner=(Spinner)findViewById(R.id.spnrmonths);
 ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(getApplicationContext(), R.array.months, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 myspinner.setAdapter(adapter);
 myspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
 // TODO Auto-generated method stub
 Toast.makeText(getApplicationContext(), "You selected "+(CharSequence) arg0.getItemAtPosition(arg2), Toast.LENGTH_SHORT).show();
  }
public void onNothingSelected(AdapterView<?> arg0) {
 // TODO Auto-generated method stub
  }
 });
 }
}

This is how the Output looks:

Initially:

When Spinner is clicked:

When an Item is selected from the list:

That’s all. Hope this article was useful.:)

 

, , , , ,

15 Comments