Tuesday, June 12, 2012

Android Spinner Example

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

In this post, you'll create a simple spinner widget that displays a list of countries and implements OnItemSelectedListener When item is selected, a toast message will display the selected item.

STEP 1: Start a new project named AndroidSpinnerExample.

STEP 2: Open the res\layout\main.xml file and insert the following code:
<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:gravity="center"
    android:orientation="vertical" >
 <Spinner 
 android:id="@+id/spinnertest" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
 android:entries="@+array/country_labels"
 />
 <Button
 android:id = "@+id/btn_OK"
 android:layout_width = "wrap_content"
 android:layout_height = "wrap_content"
 android:text = " OK "
 />
 <TextView 
    android:id="@+id/txtselecteditem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Imran khan's Android"
    android:textSize="30dp"
    />
</LinearLayout>


Android_Spinner_Example_Imran_Khan_Android Android_Spinner_Example_Imran_Khan_Android

STEP 3: Add new arrays.xml in res/values/ and put following string array:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="country_labels">
<item>Afghanistan</item>
<item>Bangladesh</item>
<item>Bhutan</item>
<item>Nepal</item>
<item>Hindustan (Bharat)</item>
<item>Iran</item>
</string-array>
</resources>

STEP 4: SpinnerActivity implements OnItemSelectedListener to get selected item from Spinner as:
public class SpinnerActivity extends Activity 
        implements OnItemSelectedListener {
 private Spinner Spinnerexp;
 private Button btn;
 private TextView txtview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinnerexp = (Spinner)findViewById(R.id.spinnertest);
        btn = (Button)findViewById(R.id.btn_OK);
        txtview=(TextView)findViewById(R.id.txtselecteditem);
        btn.setOnClickListener(ocl);
        Spinnerexp.setOnItemSelectedListener(this);
    }
    private Button.OnClickListener ocl = new Button.OnClickListener(){
     public void onClick(View v){
     String choseValue = Spinnerexp.getSelectedItem().toString();
     Toast.makeText(SpinnerActivity.this, choseValue, 
        Toast.LENGTH_SHORT).show();
     }
     };
 @Override
 public void onItemSelected(AdapterView arg0, 
          View arg1, int arg2,long arg3) {
  // Get Selected Text From Spinner
     String choseValue = Spinnerexp.getSelectedItem().toString();
     txtview.setText(choseValue);
 }
 @Override
 public void onNothingSelected(AdapterView arg0) {
  // TODO Auto-generated method stub
  
 }
}



Download Source Code: AndroidSpinnerExample









2 comments:

Mohammad Tanzeel said...

excellent example brother

Pawan said...
This comment has been removed by the author.

Post a Comment

 
Powered by Blogger