Android – Spinner example

By -

Let me give small and simple but important demo of Spinner widget. Yes it is important for the novice android programmer.

Spinner is a widget similar to a drop-down list for selecting items. In this tutorial,we will create a simple spinner widget that displays a list of days. When one is selected, a toast message will display the selected item. (FYI, I have taken this definition from


<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<RelativeLayout xmlns:android=""



package com.technotalkative.spinnerdemo;

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

public class SpinnerDemoActivity extends Activity {
    /** Called when the activity is first created. */
	Spinner spinner1;
	private static final String[] daysArray = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};

    public void onCreate(Bundle savedInstanceState) {
    //    Utils.hideTitleBar(this);


	// Method to initialize UI
    public void InitialSetUpUI()
    	spinner1 = (Spinner) findViewById(;

    	ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,daysArray);

    	spinner1.setOnItemSelectedListener(new mySpinnerListener());

    class mySpinnerListener implements Spinner.OnItemSelectedListener
		public void onItemSelected(AdapterView parent, View v, int position,
				long id) {
			// TODO Auto-generated method stub
			Toast.makeText(parent.getContext(), &quot;The day is &quot; +
			          parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();

		public void onNothingSelected(AdapterView parent) {
			// TODO Auto-generated method stub
			// Do nothing.


android spinner example

Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

  • Paritosh Anand


  • pravin

    I want get data to spinner from Mysql server Using Php and joson
    How can I do that. help me for this. post or mail me sample code

    • Paresh Mayani

      For that you have to define web service? Have you define any?

  • Paresh Mayani

    Right now you can fetch data from localhost, but sometime later you will have to upload that webservice on remote server. So you have to fetch data from webservice. Here is one of my presentation which i had presented in NitroDroid 2012 event: Web service and its integration

  • Jasmin Chauhan

    I want to work like dropdownlist where it has name and value, can i do like this in spinner?