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.

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 : https://demonuts.com/Demonuts/JsonTest/Tennis/json_parsing.php

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 GoodModel.java

Write down the below coding lines in GoodModel.java 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 MainActivtiy.java 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 GoodModel.java 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).

Watch Spinner Volley

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...

Leave a Reply

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