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="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select one item"
        android:textColor="#FF00FF"
        android:textAppearance="?android:attr/textAppearanceLarge" />

        <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/fruits"/>
  
</LinearLayout>

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"?>
<resources>
  
    <string-array name="fruit_array">
        <item>Apple</item>
        <item>Blackberry</item>
        <item>Cherry</item>
        <item>Guava</item>
        <item>Mango</item>
      
    </string-array><string name="fruits">Choose a fruit</string>
</resources>



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

package com.ipsr;

import android.app.Activity;
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. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        sp=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> arr=ArrayAdapter.createFromResource(this,R.array.fruit_array,android.R.layout.simple_dropdown_item_1line);
        sp.setAdapter(arr);
      sp.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                String item= sp.getItemAtPosition(position).toString();
                Toast.makeText(ArraywspinActivity.this,
                        "You have selected the item: " +item,
                        Toast.LENGTH_SHORT).show();
              
            }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