Android Spinner View Example

By | April 10, 2013
Android SDK Provides “android.widget.Spinner” class to create Spinner component , Spinner is a UI Component that display dropdown list of item

In this tutorial we will see how to add spinner component to the activity and when the user selects the item from dropdown list of item display the item name as toast message .

Lets See An Example

Project Detail

Project Name SpinnerDemo
Package com.example.spinnerdemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

file :- spinner_demo.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="20dp"
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=".SpinnerDemo" >

<Spinner android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignparenttop="true"
android:layout_centerhorizontal="true"
android:layout_margintop="43dp"/>

</RelativeLayout>

2. Activity

  Create an activity and set the content of the activity with spinner_demo.xmls and then get the id of the spinner .

setContentView(R.layout.spinner_demo);
sp = (Spinner) findViewById(R.id.spinner1);

 2.1 Create Array Adapter

Create ArrayAdapter Object and bind it with string array values and then set the dropdownview resource for the adapter

ArrayAdapter<String>adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_spinner_item, planets);
adapter.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item);

Set the spinner view with the above declared adapter

sp.setAdapter(adapter);

Finally the class file looks like
file:- SpinnerDemo.java
package com.example.spinnerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerDemo extends Activity {

Spinner sp;
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.spinner_demo);

sp = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,
android.R.layout.simple_spinner_item, planets);

adapter.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item);

sp.setAdapter(adapter);

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(SpinnerDemo.this,
"you selected: " + planets[position],
Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub

}
});
}
}

3. RUN

Android Spinner View

Android Spinner View

  • Hi how can i display spinner values in below the screen with radio button with fixed size with scrollbar.