Android Studio Spinner

Spinner is a select list where user can select option from a drop down list. We can use spinner on many places like when we want to ask user about his/his gender or we want to know his country name from the list of all countries.

Spinner can be populated with arrays defined in string.xml files or some time we may have to populate spinner dynamically from api data or json data. In this example we will learn how to populate spinner from json data.

For instance we assume that we have a variable named response that contains JSON data. We will write code to parse json from this json object.

Populate Spinner in android studio from JSON data.


JSONObject jsonObject = null;

//array to ids of data
List categoryArrayIds =  new ArrayList();

//array to store name 
List spinnerArray =  new ArrayList();

Int country_id; //selected country id

try {
   jsonObject = new JSONObject(response);

   JSONObject results = jsonObject.getJSONObject("results");

   JSONArray data = results.getJSONArray("data");

   //insert a blank row in spinner

   spinnerArray.add("--Select--");

   categoryArrayIds.add("0");


   for(int i = 0; i < data.length(); i++) {
     
    JSONObject jo = data.getJSONObject(i);

    String id  = jo.getString("id");

    String name  = jo.getString("name");

    spinnerArray.add(name); //insert name in spinnerArray variable

    categoryArrayIds.add(id); // insert id in spinner array variable
   }

 //use array adaper to fill the spinner

   Spinner countries = (Spinner) findViewById(R.id.countries);

  ArrayAdapter adapter = new ArrayAdapter(this, 
  android.R.layout.simple_spinner_item, spinnerArray);
  
   //set drop down resource layout file simple_spinner_dropdown_item. this layout is used when spinner is clicked.
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

  countries.setAdapter(adapter);
 
  // if you want selected any index on creation you can use below code.
  countries.setSelection(1);
} catch (JSONException e) {
    e.printStackTrace();
}


Populating Spinner with static array.

We can also populate spinner with static array defined in strings.xml file. In this example we will populate the spinner with static array using adapter.


 Spinner countries = (Spinner) findViewById(R.id.countries);

 // read the array from strings.xml file
 String[] mcountries_array = getResources().getStringArray(R.array.countries_array);

ArrayAdapter country_adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, mcountries_array);

country_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

countries.setAdapter(country_adapter);

Get the Selected value from spinner


        class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

            public void onItemSelected(AdapterView parent, View view, int pos, long id) {
               sel_category = parent.getItemAtPosition(pos).toString();

                country_id = categoryArrayIds.get(pos);
            }

            public void onNothingSelected(AdapterView parent) {
                // Do nothing.
            }
        }
      countries.setOnItemSelectedListener(new MyOnItemSelectedListener());

Leave a Reply

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

7 − three =