Android Spinner


    
 In this tutorial I'll show you three things: 
  • How to add a spinner to your application layout;
  • How to populate your spinner with an array from strings.xml file;
  • How to handle user's selection.

Lets get started then:)


1. Adding spinner to your application layout
   
           To do that you will need to add a spinner element to your xml file, which is located in the res\layout directory:
 <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=".MainActivity" >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/color_hint"  
     android:textSize="20sp" />  
   <Spinner   
     android:id="@+id/colorSpinner"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:scrollbars="vertical"  
     android:entries="@array/color_array"  />  
 </RelativeLayout>  

           As you see I have highlighted android:entries="@array/color_array" line. In this line we are defining where the entries for our spinner element are supposed to be taken from, in this case we will populate the spinner with an array. 

2. Populating Spinner with an array from strings.xml file

         The first thing that we need to do here is to create an array. Open res\values\strings.xml file and add the following code to it:

 <string-array name="color_array">  
     <item>Yellow</item>   
     <item>Pink</item>    
     <item>Purple</item>  
     <item>Red</item>       
   </string-array>  

        We now have an array that contains four items: Yellow, Pink, Purple and Red (which is kind of obvious, I know:) After creating this array we should be able to find it by its name, which we did before in our main xml file:
  android:entries="@array/color_array"   

3. Handling user's selection
         To handle user's selection we will need to edit MainActivity.java file like this:

  package com.example.spinnerapp;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.content.Context;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.AdapterView.OnItemSelectedListener;  
 import android.widget.Spinner;  
 import android.widget.Toast;  
 public class MainActivity extends Activity {  
      private Spinner ourcolorspinner;   
      private String selection;   
      final Context context = this;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           ourcolorspinner = (Spinner) findViewById(R.id.colorSpinner);  
           listenerMethod();  
      }  
      private void listenerMethod() {  
           // TODO Auto-generated method stub  
         ourcolorspinner.setOnItemSelectedListener(new OnItemSelectedListener()   
         {  
       @Override  
       public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)   
       {  
         selection = (String) parentView.getItemAtPosition(position);  
         Toast.makeText(context, "You seleceted " + selection + ".", Toast.LENGTH_LONG).show();  
       }  
       @Override  
       public void onNothingSelected(AdapterView<?> parentView)   
             {}});  
      }}  
     
        As you can see the listenerMethod() method was added. In it we are setting an OnItemSelectedListener to our Spinner and inside the onItemSelected method we inserted a code, that lets us get the value of an item that was selected as well as show a little notification about it:)

DEMO
Android Spinner example
Source Code