Spinners in android example

Spinners provide a way to select one value from a set. The spinner displays a dropdown menu with all available values, from which the user can select a new one. In the default state, a spinner shows its currently selected value.

To add a list of values to the spinner, you then need to specify a SpinnerAdapter in your Activity, which extends Adapter class.. A spinner adapter allows to  define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.

Here is a complete example that shows the usage of spinner.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_height="wrap_content" />



import android.os.Bundle;

import android.app.Activity;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

Spinner spin;

String spin_val;

String[] gender = { "Male", "Female" };//array of strings used to populate the spinner


public void onCreate(Bundle savedInstanceState) {


setContentView(R.layout.activity_main);//setting layout

spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id

//Register a callback to be invoked when an item in this AdapterView has been selected

spin.setOnItemSelectedListener(<b>new</b> OnItemSelectedListener() {


public void onItemSelected(AdapterView<?> arg0, View arg1,

int  position, long id) {

// TODO Auto-generated method stub

spin_val = gender[position];//saving the value selected



public void onNothingSelected(AdapterView<?> arg0) {

// TODO Auto-generated method stub



//setting array adaptors to spinners

//ArrayAdapter is a BaseAdapter that is backed by an array of arbitrary objects

ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender);

// setting adapters to spinners




I’m also attaching my  spinner_Demo project for your reference. 🙂


spin1spin3         spin2


2 thoughts on “Spinners in android example

  1. Thank you for another wonderful article. Where else could anybody get that kind of information in such an ideal way of writing? I’ve a presentation next week, and I’m on the look for such information.

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s