Spinner is a control or widget similar to dropdown list for selecting items.

In this article I am going to explain how create a spinner control in an android by using Spinner widget. I also explain how to add list of items in a Spinner, when one is selected, a toast message will display the selected item.

  •     Start a new project, named SpinnerDemo.
  •     Open res/values/strings.xml and add the following resource in xml:
<?xml  version="1.0" encoding="utf-8"?>

     <string name="country">Select a Country</string>
     <string name="app_name">SpinnerDemo</string>
     <string-array name="country_array">
  •      Open res/layout/main.xml and add a Spinner widget inside the linear layout:
<?xml  version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:text="@string/country" />

<Spinner android:id="@+id/spinner"
               android:prompt="@string/country" />


·     Now open the Activity file and insert the following code:

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 SpinnerActivity extends Activity {

     public void onCreate(Bundle savedInstanceState) {

         Spinner spinner=(Spinner) findViewById(R.id.spinner);
         ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this,
                     R.array.country_array, android.R.layout.simple_spinner_item);
         spinner.setOnItemSelectedListener(new OnItemSelected());

     public class OnItemSelected implements OnItemSelectedListener

            public void onItemSelected(AdapterView<?>arg0,View arg1,int arg2,long arg3)
              Toast.makeText(arg0.getContext(), "The country is
               "+arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT).show();
            public void onNothingSelected(AdapterView<?> arg0) {

The Spinner widget is captured from the layout with findViewById(int). The createFromResource() method then creates a new ArrayAdapter, which binds each item in the string array to the initial appearance for the Spinner. The R.array.country_array ID references the string-array defined above and the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform.

Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened. Finally, the ArrayAdapter is set to associate all of its items with the Spinner by calling setAdapter (adapter).

The nested class (OnItemSelected) implemented an OnItemSelectedListener interface which implemented onItemSelected (), calls when an item is selected from the spinner.


  • Run the application

Your output looks like below: 

Spinner control in Android Application

A toast message will be displayed when an item is selected from the Spinner.

  Modified On Mar-23-2018 02:06:58 AM

Leave Comment