Tuesday, May 21, 2013

How to implement spinner onItemSelected in Android


This discussion can be useful for following types of question as well
  • How an event can be fired by selecting a combo box / Drop Down List box in android? 
  • How spinner's activity can be tracked on a change of a item selection?
Assume a scenario where a spinner will hold the Name of some students and upon selecting a Name the corresponding age will be populated in a EditText. To develop this task you have to work on the following points.
  1. Implement the interface OnItemSelectedListner on the Activity class in which you have your spinner.
  2. Add two contracted method onItemSelected and onNothingSelected inside your Activity class.
  3. Do something so the Spinner gets populated when the page is created.
  4. Put some code so that the EditText get filled when the Spinner's selection has been changed.
package com.example.testproject;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

/**
 * This is the class for this activity and it implements 
 * the Interface OnItemSelectedListener
 * @author Avijit
 *
 */
public class MainActivityActivity extends Activity 
     implements OnItemSelectedListener {
 Spinner spName;
 EditText txtAge;

  List<String> listName=new ArrayList<String>();
 List<String> listAge=new ArrayList<String>();

  /**
  * onCreate method is fired for initializing
  * your activity
  */
 protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main_activity);

   spName=(Spinner)findViewById(R.id.spinnerName);
  txtAge=(EditText)findViewById(R.id.editTextAge);

   //This method populate the ArrayList<String> and then 
  //use that ArrayList to ArrayAdapter which eventually 
  //is the source of the Spinner data
  populateSpinner();
  
  //Here Spinner has been set this Class for Listen on any
  //changes by selecting any of its item which eventually 
  //trigger the method onItemSelected.
  spName.setOnItemSelectedListener(this);

  }

  public void populateSpinner(){
  listName.add("Arnold");
  listName.add("Peter");
  listName.add("John");

   ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,listName);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spName.setAdapter(adapter);

   listAge.add("14");
  listAge.add("15");
  listAge.add("16");
 }

  /**
  * This method is fired when something selected on the Spinner.
  */
 public void onItemSelected(AdapterView<?> parentView,View v,int position,long id){
  String strAge=listAge.get(position).toString();
  txtAge.setText(strAge);
 }

  public void onNothingSelected(AdapterView<?> parentView){
  //
 }

}


The main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivityActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Spinner
        android:id="@+id/spinnerName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="36dp" />

    <EditText
        android:id="@+id/editTextAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="24dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>







1 comment: