Android Spinner

Android Spinner Example

Spinner is like the combox box of AWT or Swing. It can be used to display the multiple options to the user. Only one item can be selected by the user.
In this example, we are going to display the country list. You need to use ArrayAdapter class to store the country list.

Example of Android Spinner

Let's see the simple example of spinner in android.

activity_main.xml

Drag the Spinner from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Spinner  
  8.         android:id="@+id/spinner1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="83dp" />  
  14.   
  15. </RelativeLayout>  

Activity class

Let's write the code to display item on the spinner and perform event handling.
File: MainActivity.java
  1. package com.example.spinner;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.Spinner;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity implements  
  13. AdapterView.OnItemSelectedListener {  
  14.   
  15.     String[] country = { "India""USA""China""Japan""Other",  };  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         //Getting the instance of Spinner and applying OnItemSelectedListener on it  
  22.         Spinner spin = (Spinner) findViewById(R.id.spinner1);  
  23.         spin.setOnItemSelectedListener(this);  
  24.           
  25.         //Creating the ArrayAdapter instance having the country list  
  26.         ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);  
  27.         aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  28.         //Setting the ArrayAdapter data on the Spinner  
  29.         spin.setAdapter(aa);  
  30.     }  
  31.   
  32.       
  33.     //Performing action onItemSelected and onNothing selected  
  34.     @Override  
  35.     public void onItemSelected(AdapterView arg0, View arg1, int position,long id) {  
  36.         Toast.makeText(getApplicationContext(),country[position] ,Toast.LENGTH_LONG).show();  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onNothingSelected(AdapterView arg0) {  
  41.         // TODO Auto-generated method stub  
  42.           
  43.     }  
  44.   
  45.     @Override  
  46.     public boolean onCreateOptionsMenu(Menu menu) {  
  47.         // Inflate the menu; this adds items to the action bar if it is present.  
  48.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  49.         return true;  
  50.     }  
  51. }  


Output:

android spinner example output 1 android spinner example output 2 android spinner example output 3

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international voip calls