In this tutorial we will create an Android Spinner Example Application. So the first thing probably you are thinking is What is Spinner? Actually Spinner is used to show a dropdown list. So in this Android Spinner Example Tutorial we will create a spinner. And in the spinner we will show the values from MySQL database.
Android Spinner Example Video
Before going further you can check out this video to see the final output of the app we are creating.
Now if you wanna create the same, lets begin this Android Spinner Example Tutorial. So in this tutorial we will retrieve data from our webserver using JSON String. I have my URL that will give me a JSON string by fetching values from MySQL database.
http://simplifiedcoding.16mb.com/Spinner/json.php
The above link will give us the following json string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | { "result":[ { "username":"faiz", "name":"Faiz Khan", "course":"BCA", "session":"2014-2017" }, { "username":"belal", "name":"Belal Khan", "course":"MCA", "session":"2015-2018" }, { "username":"ramiz", "name":"Ramiz Khan", "course":"MBA", "session":"2015-2017" }, { "username":"zafar", "name":"Zafar Khan", "course":"MBA", "session":"2014-2016" } ] } |
You can create your own url or you can also use mine. Lets move to Android Studio now.
Creating Android Spinner Example Application
- Open Android Studio and create new Project.
- For this tutorial we will use volley library so add the following code in you build.gradle dependencies
1 2 3 4 5 6 7 8 9 10 | dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' //Volley Library compile 'com.mcxiaoke.volley:library-aar:1.0.0' } |
- Now sync your project with gradle.
- Come to activity_main.xml and create the following layout.

- In the above layout we have a Spinner and 3 TextViews. When user will select an item from the Spinner the details will be shown to the TextView.
- You can use the following xml code for creating the above layout. Define Spinner and 3 TextViews.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner" /> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:layout_marginRight="20dp" android:layout_marginLeft="10dp" android:text="Name" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:layout_marginRight="20dp" android:text="Course" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:layout_marginRight="20dp" android:text="Session" /> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_marginLeft="10dp" android:id="@+id/textViewName" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:id="@+id/textViewCourse" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:id="@+id/textViewSession" /> </TableRow> </TableLayout> </LinearLayout> |
- Create a class named Config.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package net.simplifiedcoding.spinnerexample; /** * Created by Belal on 10/31/2015. */ public class Config { //JSON URL public static final String DATA_URL = "http://simplifiedcoding.16mb.com/Spinner/json.php"; //Tags used in the JSON String public static final String TAG_USERNAME = "username"; public static final String TAG_NAME = "name"; public static final String TAG_COURSE = "course"; public static final String TAG_SESSION = "session"; //JSON array name public static final String JSON_ARRAY = "result"; } |
- Now come to MainActivity.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | package net.simplifiedcoding.spinnerexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener{ //Declaring an Spinner private Spinner spinner; //An ArrayList for Spinner Items private ArrayList<String> students; //JSON Array private JSONArray result; //TextViews to display details private TextView textViewName; private TextView textViewCourse; private TextView textViewSession; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Initializing the ArrayList students = new ArrayList<String>(); //Initializing Spinner spinner = (Spinner) findViewById(R.id.spinner); //Adding an Item Selected Listener to our Spinner //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener spinner.setOnItemSelectedListener(this); //Initializing TextViews textViewName = (TextView) findViewById(R.id.textViewName); textViewCourse = (TextView) findViewById(R.id.textViewCourse); textViewSession = (TextView) findViewById(R.id.textViewSession); //This method will fetch the data from the URL getData(); } private void getData(){ //Creating a string request StringRequest stringRequest = new StringRequest(Config.DATA_URL, new Response.Listener<String>() { @Override public void onResponse(String response) { JSONObject j = null; try { //Parsing the fetched Json String to JSON Object j = new JSONObject(response); //Storing the Array of JSON String to our JSON Array result = j.getJSONArray(Config.JSON_ARRAY); //Calling method getStudents to get the students from the JSON Array getStudents(result); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); //Creating a request queue RequestQueue requestQueue = Volley.newRequestQueue(this); //Adding request to the queue requestQueue.add(stringRequest); } private void getStudents(JSONArray j){ //Traversing through all the items in the json array for(int i=0;i<j.length();i++){ try { //Getting json object JSONObject json = j.getJSONObject(i); //Adding the name of the student to array list students.add(json.getString(Config.TAG_USERNAME)); } catch (JSONException e) { e.printStackTrace(); } } //Setting adapter to show the items in the spinner spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students)); } //Method to get student name of a particular position private String getName(int position){ String name=""; try { //Getting object of given index JSONObject json = result.getJSONObject(position); //Fetching name from that object name = json.getString(Config.TAG_NAME); } catch (JSONException e) { e.printStackTrace(); } //Returning the name return name; } //Doing the same with this method as we did with getName() private String getCourse(int position){ String course=""; try { JSONObject json = result.getJSONObject(position); course = json.getString(Config.TAG_COURSE); } catch (JSONException e) { e.printStackTrace(); } return course; } //Doing the same with this method as we did with getName() private String getSession(int position){ String session=""; try { JSONObject json = result.getJSONObject(position); session = json.getString(Config.TAG_SESSION); } catch (JSONException e) { e.printStackTrace(); } return session; } //this method will execute when we pic an item from the spinner @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //Setting the values to textviews for a selected item textViewName.setText(getName(position)); textViewCourse.setText(getCourse(position)); textViewSession.setText(getSession(position)); } //When no item is selected this method would execute @Override public void onNothingSelected(AdapterView<?> parent) { textViewName.setText(""); textViewCourse.setText(""); textViewSession.setText(""); } } |
- Finally before running this Android Spinner Example , add internet permission to your manifest file.
1 2 3 | <uses-permission android:name="android.permission.INTERNET"/> |
- Now run your application.

- Bingo! Our Android Spinner Example App is working absolutely fine. You can also download my source code from below.
Get source of Android Spinner Example
So thats all for this Android Spinner Example Tutorial friend. This was a very simple android spinner example. Please do comment if you are having any trouble or queries. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.
Get i can the php file?
Get it from here
https://drive.google.com/file/d/0B13EE_qsNQLpSWZhM0pUU0xXb2c/view?usp=sharing
Can I have the PHP file ?
Thank you so much sir! You saved my life!
Thank you very much for this codes ,thank you
Nice example,
But in my code i need to load first spinner with name of city and when ever i click on city i need to load another spinner w.r.t city id . can help me in two spinners?
Please
I have same problem with you. and how to get String from OnItemSelected position so I can implement it on other method to load other JSON. Untill now I just can get string with “Textview.setText(getName(position))” and to get String on another method I use “String name = Textview.getText().toString()”. Anyone can help me to get String without implement it to Textview?
I think it’s great your tutorial, and I give many thanks , but , you could explain how this Json to have several lines in the table? Thanks for your answer.
{“result”:
[{“Username”:”Bayarri Carmona,Alex”,
“Course”:{“Session”:”2″, “Name”:”0″}},
{“Username”:”Borras Vicent,Antonio Salvador”,
“Course”:{“Session”:”0″, “Name”:”0″}}]}
the code is not working showing null pointer exception in getstudents
Have you tried downloading my source code? Tell me exactly in which line you are getting a null pointer?
GREAT TUTORIAL!! thank you Belal Khan …
need tutorial for spinner to save the spinner values to mysql database.. thank you
Thank you for this tutorial,
Have you any tutorial or example,i need onclick on spinner item to show result into a listview passing the id from the spinner to the listview?
can anyone help me?
thanks
Assalamualekum Bilal Bhai……I want to call API data on select item in spinner. How to do plz help?
belal i want php script for change pswd+android code using volley only…..
I done login and registration task using androidhive example… your will be important…..
your help will be important
hi… will u plzz help me… i am using a viewpager and feeding data through json parsing using volley.. I am doing this using a fragment and adapter..My volley response code is in the fragment.. can u help me to define the spinner in the fragment..?? plzz help
This is nice code..
I want to this data show in another spinner.
so please help me..??
hello Belal,
thank you for this tutorial.
i got the spinner working with my code , yet i need the item selected from the spinner to be stored in an edit text field instead of a text view and that is to use it later to submit its value to Server .
my problem is that i am facing a difficulty in having the edit text field empty by default . it always contain the first spinner item in it
how can i solve that
thank you in advance
I have 2 spinner. so should i do getData1() and getStudents1()…??? please reply buddy 🙂
I have 2 spinner. How can i implement with 2 spinner ? so should i do getData1() and getStudents1()…??? please reply buddy 🙂
Well Explained I just Tried and Its working smoothly after troubleshoot some bugs .But I want to maintain the last selected value in spinner after starting the app instead of selecting again and again Can any one suggest what need to do .
Heyy Thank you so much for this tutorial it’s really helpful sir but i want to have a listview of that if you can do a tutorial about that it will be so helpful for me for example if we have different student with the same username i want to list me students Pleaaase sir !!thanks in advance
I am not getting my json data in spinner 🙁 .any solution . thanks in advance
Did you get any help?I have got the same problem.
belal how i can load data in listview based spinner clicked id…. i have tried but getting error….. will you guide me??
Removing duplicates in a Spinner ?
can we implement this concept using retrofit!
This code is giving null Pointer Exception for line no.-60 and 63.Please tell me how to resolve it
how to get data from mysql data in spinner and get data separately for every item of spinner from mysql data in to listView using CodeIgnither framework instead of php script
thanks
11-25 16:25:00.088 2115-2115/net.simplifiedcoding.spinnerexample W/System.err: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
11-25 16:25:00.089 2115-2115/net.simplifiedcoding.spinnerexample W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
how to resolve this
Hi,
how to set empty selection before click the spinner?
getting error of Dropbox when i click on download link plz help me
please post multiple selection spinner with checkbox using json
link broken, where i can download
hi, i am using fragment class,cannot update details in spinner whenever i am update in server.
How to get duplicate row or muliple row ??
Make the json as array n fetch it..
Make json as array n fetch it.
I want to filter my listview, can you give me some tips on what changes i need to do?
use filter concept
could give a tutorial when selecting items in the spinner, the new data will appear in a second spinner, a spinner with parameters. Thank you
Hey Belal,
Thank you for the tutorial. I got it to work on a fragment after two days of searching.
How to implement when no array name in json sting
[
{
“username”:”faiz”,
“name”:”Faiz Khan”,
“course”:”BCA”,
“session”:”2014-2017″
},
{
“username”:”belal”,
“name”:”Belal Khan”,
“course”:”MCA”,
“session”:”2015-2018″
}
]
You can simply convert this string to a json array…
JSONArray yourarrayname = new JSONArray(yourjsonstring);
Thanks for reply. I am new to android, It will be great if you rerwrite the MainActivity.java and send us.
Very nice guide. But when I am trying to implement this in a fragment, the data is downloading but not showing in spinner by default. When clicking the spinner the list shows, but the spinner click listener doesn’t work as well. Any guide to point me would be very helpful.
if u got the code of mainactivity plz send me
mail:pradeeppuru44@gmail.com
thanks in advance
hallo belal
your tutorial is very easy to understand
but when I try I get an error in this section
RequestQueue requestQueue = Volley.newRequestQueue (this);
in part (this) is red. how do i solve it?
if you are using fragments than use getActivity or getContext instead of this.
but if my json hasn’t name ?
if two searchable spinner in one class ? How About That ?
hii,,
I have followed above steps and app is running, there is no error. but it displaying only blank spinner.
Hey Bilal please make one tutorial on spinner with volley mysql data with multiselect using checkbox..
sir, i cant download ur source code sir
I will update the links soon. Meanwhile you can check my github repository
github.com/probelalkhan