Android Populate Spinner From JSON Using Volley Example

Welcome to Android Populate Spinner From JSON Using Volley Example Tutorial.

In this tutorial, we will populate android spinner from mysql database using volley and JSON data.

To load the android spinner with json array using volley, we need to call web service.

We will fetch the JSON data from MySQL database and remote server using web service.

JSON array data will be in string format and we will convert them into string array first.

Watch Spinner Volley

Below steps will help you to make android spinner example to load json using volley.

Step 1. JSON Texts

Following is the JSON data, which we will use in this example.

Here is the link to the web service :

Step 2. Gradle and Manifest

Now we will edit the build.gradle (Module :app) file.

Add the below line in build.gradle (Module :app) file.

This line will fetch required classes from volley library that we need to use for web service calling.

Now add the following line in the AndroidManifest.xml file

Above line is the internet permissions.

This permissions allows us to use the internet from android device.

Step 3. Image and drawable file

In our spinner, we will use customize dropdown icon.

For this we need one image of dropdown arrow which you can download by clicking the below link

Download arrow Image

Add this image to the res->drawable directory.

Now time to create one XML file in the same directory. Give it a name spinner.xml

Write down the below source code in  spinner.xml file

Above file will draw the border to our spinner.

The color of the borders will be pink (colorAccent from the colors.xml)

You can change the color of the borders using the below line

You can also change the size or width of the border using the above line.

To change the corner radius of the border, you can use below line

Increase the above value to get more width of the border.

To set the custom dropdown icon, you need to use the below code lines

Using gravity, you can set the place where you want to put dropdown arrow. I have put it at center and right.

Use src to give reference to the arrow image.

Step 4.  Class of Model

Time to create the model class which will help to maintain the data in proper manner.

Prepare a new class and give it a name

Write down the below coding lines in file

We will get the information like player name, country, city and image url.

Above class includes the getter and setter methods for all the information types.

Step 5. Last Works

Our final task is to edit main files of our project.

In your activity_main.xml file, add the below code

One textview and one Spinner is there in above file.

Textview is just for reference. Spinner is the thing to take into consideration.

I have set the spinner.xml file as the background of this spinner.

spinner.xml file will help us to make attractive spinner with borders, corners and custom dropdown icon.

Source code for file is like the below snippet

Learning the Main Code

Look at the following source lines

First line is the url which we need to call for the json parsing.

Second line is the object of the ProgressDialog class.

This object will help us to create loading dialog while we are fetching the web service.

Third line is the arraylist with the objects of the GoodModel class.

Fourth line is also an arraylist but with the strings.

Last line is the simple object of the spinner.

Compiler will call the retrieveJSON() method in the onCreate() method.

Code block for retrieveJSON() method is as the below

Compiler will show the progress dialog first.

Then it will create the object of the StringRequest class.

Here, we will use the URL to the web service.

After getting the JSON data from remote server, compiler will call the onResponse() method.

Here, it will first parse the json and then it will iterate one for loop.

In the every iteration, it will create one object of the class and then will assign information to this object.

After this for loop, it will create another for loop where it will fill the string array (names).

Then it will populate the spinner using the string array (names).

Download Source Code For Android Populate Spinner from JSON using Volley

Download Source Code For Spinner_Json_Volley

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

9 Responses

  1. raj says:

    please post php code also if possible..
    Tutorial is very helpfull.

    • Hardik Parsania says:

      Here are the coding lines for json_parsing.php

      while ($row = mysqli_fetch_assoc($result)) {
      $emparray[] = $row;
      echo json_encode(array( “status” => “true”,”message” => “Data fetched successfully!”,”data” => $emparray) );

  2. gickel says:

    please how can i get the spinner value in a variable ?

  3. isseke says:

    Please how can i store a spinner selected value in a variable ?

  4. Deepak says:

    Excellent tutorial with great explanation.

  5. Alfan says:

    hello, why on the spinner i can’t add params for parsing selected data, and the value not showing
    please help me, thanks

  6. herman | jakarta | indonesia says:

    HI Bro, easy to learn. Thx
    Could u please write tutorial about search autocomplete with json and volley ?
    for example search for name

  7. Pedro says:

    How can you clear the array adapter if you are calling it again after?
    I cant seem to clear the spinner at all, it appends more items.

Leave a Reply

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