Browse By

Android: Spinner (Drop-Down List) Example in Android Studio 2.0

Hello, Android developers…

In the last article, we have learned how to create AutoCompleteTextView in Android Project.

We have already created Spinner (Drop-Down List) example in Eclipse. But, today we are going to create a simple Spinner (Drop-Down List) example in Android Studio 2.0. :-)

What is Spinner in Android?

Spinner is a widget similar to a drop down list for selecting items. Spinner allows you to select an item from a drop down menu. It provides an easy way to select the value from a list. Clicking spinner will display a drop-down list with the available values.

You can use “android.widget.Spinner” class to render a dropdown box selection. It provides a quick way to select one value from a set. In the default state, a spinner shows its currently selected value.

In this tutorial, we will create a simple android application which will demonstrate two ways of assigning values to the spinner.

First spinner (Country Spinner) will get value from a string.xml file and another spinner (Business Type) will get value dynamically.

  1. Using XML layout: Render a Spinner in XML, and load the spinner items via XML file.
  2. Programmatically or Dynamically: Render another Spinner in XML, and load the spinner items programmatically.

Attach a listener to both Spinner to show the selected items to the user.

Direct Download Full Source Code


Android: Spinner (Drop-Down List) Example in Android Studio 2.0

  1. Create a new project and fill the required details File => New => New Project

After Creating Project, open res –> values –>strings.xml. Initialize string array in it. It Will display the first spinner using XML, so let’s create a string array in string.xml.

Our second spinner will populate list dynamically, so we will need to create a string array in our file. Let’s create a String array dynamically.

2. Open content_main.xml from layout folder and add Spinner control in it as follows:


Some important properties of spinner are:

  • android: prompt

The prompt to display when the spinner’s dialog is shown.

  • android:spinnerMode

It defines display mode for spinner options. This Property has two options: Dialog mode and DropDown mode. Dialog mode uses a dialog window for selecting spinner options and drop-down mode uses drop-down anchored to the Spinner for selecting spinner options.

  • android: entries

It references to an array resource that will populate the Spinner. In first spinner(country-spinner), we have used string array to populate spinner items via using entries property of spinner. We also have defined two different styles for spinners.

3. Now open your

  • Initialize the Spinner control in it.
  • Create an array adapter for storing spinner items.
public class MainActivity extends AppCompatActivity  {

    // uicontrols
    Spinner spCountries;
    Spinner spBusinessType;
    Button btnsubmit;

    //class members
    String businessType[] = { "Automobile", "Food", "Computers", "Education",
            "Personal", "Travel" };
    ArrayAdapter<String> adapterBusinessType;

    // local members
    String sbusinesstype,scountry;

    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(;

        spCountries = (Spinner) findViewById(;
        spBusinessType = (Spinner) findViewById(;



As our first spinner will populate items via XML so there is no need to create an adapter for it but when we populate list programmatically we have to create an array adapter. So let’s create an Array Adapter. 

So let’s create an Array Adapter. We will use default array-adapter:

adapterBusinessType = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, businessType);

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

1) context: Pass current context in it.
2) resource: The resource ID for a layout file containing a TextView to use when instantiating views.
3) objects: The objects to represent in the ListView. Pass list into it.

So , in onCreate() method, add below lines of code.

protected void onCreate(Bundle savedInstanceState) {
// Initialize and set Adapter
adapterBusinessType = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, businessType);



The simple_spinner_item layout defines how the selected choice appears in the spinner control. It is provided by the android platform and is the default layout you should use unless you’d like to define your own layout for the spinner’s appearance.

Dropdown specify the layout , adapter should use to display the list of spinner choices. simple_spinner_dropdown_item is another standard layout defined by the platform. setAdapter() is used to apply the adapter to your Spinner.

4. Handle the Spinner click event using setOnItemSelectedListener() method. Place below lines of code in onCreate() method.

spCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> adapter, View v,
                               int position, long id) {
        // On selecting a spinner item
         scountry = adapter.getItemAtPosition(position).toString();
        // Showing selected spinner item
                "Selected Country : " + scountry, Toast.LENGTH_SHORT).show();
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub


5. Now Execute the application

This slideshow requires JavaScript.


I hope you like this article. Share your reviews to improve content. Please stay tune with us. In next article, we will learn Android Radio button and Radio Group Example in Android Studio 2.0. Happy coding!!

Subscribe for more articles!!

Download Full Source Code