Android : Spinner

What to do when you have a list and want the user to select one item amongst it. Spinners make this task simpler. When the user clicks on that element they drop down like a list. The user can select one of the items and continuous. Thus, they avoid blocking your view and using unnecessary screen space. Spinner is a view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view. So here in this Android Series, we learn how to implement these Spinners. So, here I have an example where users have to select his gender and Android version and click the button which launches a new activity to display the user selections. To switch between we use Intents and to pass the user selections of these spinners to the next activity we use SharedPreferences. To learn how this is done you can refer to my tutorial on Intents and Shared Preferences. If you find this hard then for now just display the user’ s selection in the same activity. So, let’s get on with Spinners. In our XML file, we first drag and drop two spinner widgets. Now we need to specify each with the array of items to populate with. This is done by the attribute android:entries=” “. Don’t forget to declare that array in your strings.xml file and specify those elements in that array.


Screenshot_2015-01-02-17-32-41 In the main activity, declare two Spinners, initialise their views. Now we have to do is to set on item select listener which will identify the item selected by the user.

setOnItemSelectedListener(new CustomOnItemSelectedListener());

The setOnItemSelectedListener() registers a callback to be invoked when an item in this AdapterView has been selected. The CustomOnItemSelectedListener() is an interface implementing OnItemSelectedListener. The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The onItemSelected() callback method is invoked when an item in this view has been selected. This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item. Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item. The onNothingSelected() callback method is to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter becomes empty. The item selected by the user can be retrieved by getSelectedItem() which returns the data corresponding to the currently selected item, or null if there is nothing selected. This can be displayed in a TextView. Screenshot_2015-01-02-17-32-57

//Main Activity
package com.shirish.Intent;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
   Button next;
   Spinner gender,android;
   Editor editor;
 protected void onCreate(Bundle savedInstanceState) {
     next=(Button) findViewById(;
     setTitle("Intent 1");
     SharedPreferences pref = getApplicationContext().getSharedPreferences("Options", MODE_PRIVATE);
     gender.setOnItemSelectedListener(new CustomOnItemSelectedListener());
     next.setOnClickListener(new OnClickListener(){
         public void onClick(View v){
            String gen=gender.getSelectedItem().toString();
            String and=android.getSelectedItem().toString();
            editor.putString("gender", gen);
            editor.putString("android", and);
            Toast.makeText(getApplicationContext(), "Settings saved!",Toast.LENGTH_LONG ).show();
            Intent in=new Intent(MainActivity.this,NextActivity.class);

//CustomOnItemSelectedListener Interface
package com.shirish.Intent;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {  }

  public void onNothingSelected(AdapterView<?> parent) {  }

Download The Source code here:








4 responses to “Android : Spinner”

  1. Android : Intents and Shared Preferences | SHIRISH KADAM Avatar

    […] pick among them. The items in the Spinner come from the Adapter associated with this view. Read my Spinner tutorial to learn how Spinners are […]


  2. Mayur Jadhav Avatar
    Mayur Jadhav

    why im not able to download this code????????


  3. Mayur Jadhav Avatar
    Mayur Jadhav

    please send me the above project code on my email id


    1. Shirish Kadam Avatar

      Hi Mayur thank you for informing the broken link!. I have updated the link please click on the Download image and save the source code.


Leave a Comment:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Powered by

%d bloggers like this: