---
title: "Spinner control in Android Application"  
description: "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"  
author: "Chris Anderson"  
published: 2011-10-22  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/790/spinner-control-in-android-application  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Spinner control in Android Application

Spinner is a control or widget similar to [dropdown list](https://www.mindstick.com/forum/760/dropdown-list-open-hide-behind-another-dropdown-list-on-ie-7) 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](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful), named SpinnerDemo.
- Open res/values/strings.xml and add the following [resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) in xml:

```
<?xml  version="1.0" encoding="utf-8"?>
<resources>
     <string name="country">Select a Country</string>
     <string name="app_name">SpinnerDemo</string>
     <string-array name="country_array">
         <item>India</item>
         <item>USA</item>
         <item>Australlia</item>
         <item>Newzeland</item>
         <item>Italy</item>
         <item>France</item>
         <item>England</item>
         <item>Srilanka</item>
     </string-array>
</resources>
```

- 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:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
<TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="10dip"
     android:text="@string/country"  />

<Spinner  android:id="@+id/spinner"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:prompt="@string/country" />
</LinearLayout>
```

· Now open the [Activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android) 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 {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         Spinner spinner=(Spinner) findViewById(R.id.spinner);
         ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this,
                      R.array.
                     R.array.country_array, android.R.layout.simple_spinner_item);
         spinner.setAdapter(adapter);
         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](https://www.mindstick.com/forum/12803/check-for-any-item-in-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](https://www.mindstick.com/articles/23223/naming-convention-or-coding-standard) spinner appearance, defined by the platform.

Then [setDropDownViewResource(int)](http://developer.android.com/reference/android/widget/ArrayAdapter.html#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](https://www.mindstick.com/articles/12103/nested-classes-in-java-classifications-of-nested-classes) (OnItemSelected) implemented an OnItemSelectedListener [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) which implemented onItemSelected (), calls when an item is selected from the spinner.

- Run the [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)

Your output looks like below: \

![Spinner control in Android Application](https://www.mindstick.com/mindstickarticle/3e222f3b-667a-4489-95af-9c51532f3ee0/images/124fb381-cc06-43df-81b3-09336af34b2f.png)

A toast message will be displayed when an item is selected from the Spinner.

---

Original Source: https://www.mindstick.com/articles/790/spinner-control-in-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
