Spinner in Android

In this tutorial we are going to learn and explore about Spinner in Android. So let's start by understanding what is a Spinner?

A Spinner is a type of view that hold items in form of a dropdown menu available for user selection. It creates a menu with multiple options where a user can select any one option. Following is an example of a spinner.

Adapter

We can create a Spinner, by adding the following code to our layout XML file:

<Spinner
    android:id="@+id/days_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

What's the difference between Spinner and ListView?

Both ListView and Spinner looks very similar. But they differ from each other.

Spinners provide a quick way to select one value from a given set of values and in the default state, a spinner only shows the currently selected value. When you touch(tap on) the spinner, it displays a dropdown menu with all other available values(options), from which the user can select a new one.

ListView, on the other hand, is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a data source such as an array or database table and converts each dataitem into a view that is placed in the list.

Therefore, a Spinner and ListView differs in the way they appear and in their usage too. If you want to select only one value from a set of options then you should use a Spinner. If you want to display a list of data then use a list view.


How does a Spinner works?

  1. Define the data source

    There should be a data source that will be used to display the data in the spinner.

    String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    A data source can be a list, an array, a JSON data or data coming from a database.


  2. Define the Adapter

    There will be an Adapter(ArrayAdapter etc) to take data from data source, create a view of it and then pass it to the AdapterView i.e. Spinner. Thus, you need to tell the Adapter how to display the data by specifying a layout for the drop down.

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

    There are 3 parameters used to instantiate an ArrayAdapter(we have used an ArrayAdapter in our example):

    • Context c: Refers to the current class object where ArrayAdapter is being instantiated.
    • Layout: It is a layout file that defines how a single item will appear in the spinner. Android SDK by default provides layouts like simple_spinner_item and simple_spinner_dropdown_item and if you do not have any special design requirements, we suggest you use these only.
    • Data source: It is the data source from which data will be converted into a view.

  3. Define any actions for Spinner

    To add an event handler for option selection, you can implement OnItemSelectedListener interface, to determine what happens when a user selects any option from the menu. For this, you need to implement the above mentioned interface and override two methods:

    onItemSelected()

    This method has 4 parameters:

    • AdapterView av: It's the Spinner view that you have used.
    • View v: It defines the TextView inside the spinner that was clicked.
    • int position: It tells the position of the item that was clicked in the Spinner. The index or the position starts from 0.
    • long id: It gives the row id of the item clicked in the Spinner. This parameter is mainly used when dealing with databases in Android.

    onNothingSelected()

    This method has only 1 parameter:

    AdapterView av: It's the Spinner view that you have used. This method is called whenever the currently selected item is removed from the list of available items in the Spinner. If the adapter is modified such that the currently selected item is no longer available, then this method will be called. This method may be used so that you can set which item will be selected when the previous item is no longer available. This prevents the spinner from automatically selecting the next item in the list.

    If mySpinner is an instance of a Spinner view then, following is how we can implement the above listeners:

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
        { 
            // An item was selected. You can retrieve the selected item using
            // parent.getItemAtPosition(pos)
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) 
        { 
            // If an option is removed then what to do
            // or anything else
        }
    
    });

We hope you have got an idea of what Spinners are and how they can be implemented. In the next tutorial, we will see how we can create a Spinner in our Android Application.