Multiple Spinner Dropdown in Android

Spinners are drop-down menu’s which is used for making a selection from a number of options. It is mostly used when there are many options like choosing the country, state, network operator etc.

This tutorial is about how can we display multiple spinners/composite spinners. That is, how to populate one spinner based on the choice of the other spinner. In this tutorial, I will be demonstrating how city details are populated when a state is chosen. Here I have made use of Volley library for downloading data from the server.

The JSON Structure

I have placed the JSON data that will be used in this tutorial here: http://api.androiddeft.com/cities/cities_array.json.  It contains an array of JSON Objects. Each JSON Object holds single state details. A sample JSON structure is shown below.:

Creating the Project and Adding Dependencies

1. Create a project in Android Studio with the name Spinner Demo.

2. Add the Volley library dependency in the build.gradle file of the app module. Volley dependency is used for downloading the state and city details from the server. It is not necessary for implementing spinners.

3. In AndroidManifest.xml file, add Internet permission for Volley

Customizing the Spinner

4. Create a layout file named state_list.xml and add the following code. This is used to style the items within the spinner.

5. Similarly, create city_list.xml and add the following code:

6. Create a POJO class called State.java to store the state and city details:

7. Add a custom adapter named StateAdapter for handling state spinner. This helps in holding city details corresponding to each state.

8. As we have seen in the Volley tutorial, add a singleton class, MySingleton.java

Handling the Spinner in Main Activity

8. Open activity_main.xml and add the following code. It has 2 spinners, one for states and another for cities, and a submit button. When the submit button is pressed, we will display the state name and city name selected.

9. Finally, update MainActivity with the below code. It displays a loader when the data is being downloaded from the internet, then populates the state and city names in the corresponding spinners. When the user changes the state, it dynamically updates the cities spinner. When the Submit button is clicked, currently selected state and city names are displayed.

Demonstration

Now run the application and you can see the app as shown below:

Spinner in Android Demo

Source code and APK files

You can download the Source code and APK files from the below links.

Download Source CodeDownload APK

If you have any doubts, then we can discuss them in the comments section. If you have liked this tutorial, please do follow us on Twitter and Facebook.

Stay Connected

No spam guarantee.

Abhishek

Abhishek

Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.
Abhishek

Leave a Reply

Your email address will not be published. Required fields are marked *