Thursday, 29 March 2012

Spinner in Android

Spinner is a widget similar to the drop-down list for selecting items.

Example program to display the fruits list using spinner.

1.Create one class "arraywspin"
2.Open the res/layout/main.xml file and insert the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="@string/hello" />

        android:text="Select one item"
        android:textAppearance="?android:attr/textAppearanceLarge" />


Notice that the TextView's android:text attribute and the Spinner's android:prompt attribute both reference the same string resource. This text behaves as a title for the widget. When applied to the Spinner, the title text will appear in the selection dialog that appears upon selecting the widget.

3.Create array.xml in res/values and edit the file like following codes to display the items.

<?xml version="1.0" encoding="utf-8"?>
    <string-array name="fruit_array">
    </string-array><string name="fruits">Choose a fruit</string>

4..then open the ,and insert the following coding to display the spinner.

package com.ipsr;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class ArraywspinActivity extends Activity {
    Spinner sp;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        ArrayAdapter<CharSequence> arr=ArrayAdapter.createFromResource(this,R.array.fruit_array,android.R.layout.simple_dropdown_item_1line);
      sp.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                String item= sp.getItemAtPosition(position).toString();
                        "You have selected the item: " +item,
            }public void onNothingSelected(AdapterView<?> arg0) {

After the main.xml layout is set as the content view, the Spinner widget is captured from the layout with findViewById(int). The createFromResource() method then creates a new ArrayAdapter, which binds each item in the string array to the initial appearance for the Spinner (which is how each item will appear in the spinner when selected).  Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform). Finally, the ArrayAdapter is set to associate all of its items with the Spinner by calling setAdapter(T).

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored.

5.Then run the program.

No comments:

Post a Comment