Android - Get Selected Item from Spinner Example - ANDROID - Helper

Wednesday, September 28, 2011

Android - Get Selected Item from Spinner Example

GET SELECTED ITEM FROM SPINNER


SOURCE CODE [main.xml] is


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

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

<Spinner android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:layout_width="fill_parent" />

</LinearLayout>


SOURCE CODE [SpinnerExample.java] is


package com.SpinnerExample;

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 SpinnerExample extends Activity
{
                Spinner sp;
                ArrayAdapter<String> adapter;
                String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
                                                "EIGHT", "NINE", "TEN" };

                public void onCreate(Bundle savedInstanceState)
{

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                sp = (Spinner) findViewById(R.id.spinner1);

                                adapter = new ArrayAdapter<String>(this,
                                                                android.R.layout.simple_spinner_item, numbers);
                                sp.setAdapter(adapter);
                               
                                sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
                                                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                                                                int arg2, long arg3)
{
Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();                                                 
                                                }
                                                public void onNothingSelected(AdapterView<?> arg0)
{
                                                                // TODO Auto-generated method stub                 
                                                }
});
                }
}


The OUTPUT will be

 https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTlEg_I14b8i02aA-XTg8aPH2Gl838cssnWAMM1LzXQevOu0uh8Q9dsQLGCemBPdYmUbXbVY5akX9cMU_EttHOCfwWU8JDRFDHdLdtS59eHjyPvruoqgypNgsDPcuiTZxVZ8jv1FUM-EY/

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZ5tyWBq9ngM_613TuqIQqRtqJ75qcW5_UA8dAVChdJmsN48v3PFcNdYiKzZlCPHYoUYo5AlfcuAiIU8nV6NWRhNPaRlAW90EOf3LIIL1ImxTz8YLLJVi-xZ9V0M994Yh3-jtGoXj4JtY/

2 comments:

  1. How to work with 2 spinners, both spinners will have same value. Once we click on 1st spinner item, then that item should not show in 2nd spinner???
    Plz forward this answer....

    ReplyDelete
  2. I have 2 options in the spinner 1 & 2. When I run the application by default it shows 1 at first time and then if i select 2 then it shows 2 as selection. I don't want this...Means i want user to select the option and then accordingly selection must be done......Plz do rply as early as possible.

    ReplyDelete