ZOFTINO.COM

Android Spinner Dropdown List

Spinner widget is a drop down list in android. Using spinner, your app can display list of available choices for a particular data element and allow user to select one item from the list.

In this article, I’ll explain populating various formats of data in spinner, properties of spinner, spinner customization, and spinner listener with examples.

Defining Spinner in Layout

To use spinner, first it needs to be defined in layout using Spinner element. Below layout shows how to use spinner in constraint layout.

 

    

Populating Spinner from String Array Xml

Since spinner populates multiple items like list view, spinner can be populated using adapter. Adapter maps view and data. Below code shows how to populate spinner from string array xml using ArrayAdapter.

Layout for each item and string array, and drop down view are passed to array adapter.

 spinnerOfferType = (Spinner)findViewById(R.id.spinnerOfferType);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.offer_types, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerOfferType.setAdapter(adapter);
android spinner example

Populating Spinner from ArrayList or Programmatically or Dynamically

To populate spinner with data from list of strings, you need to use ArrayAdapter constructor passing item view and ArrayList as shown below.

 spinnerOfferType = (Spinner)findViewById(R.id.spinnerOfferType);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, CouponStoreData.getCouponData());
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerOfferType.setAdapter(arrayAdapter);

Populating Spinner from Database SQLite

If you want to know how to select, insert, update and delete data in SQLite, you can read my previous tutorial how to use SQLite database in android.

Here I’ll show how to convert convert cursor to string array list to populate it in spinner.

 public List<String> getOfferTypes(){
    List<String> offerTypes = new ArrayList<String>();


    String selectQuery = "SELECT  * FROM " + TABLE_OFFERTYPES;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);


    if (cursor.moveToFirst()) {
        do {
            offerTypes.add(cursor.getString(0));
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    return offerTypes;
}

Populating Spinner from JSON

Json data can be populated in spinner by converting JSON into string array list. In this example I am going to show how to convert JSON into string array list and populate it in spinner.

 //json string
String offerJson = "{ \"offerTypes\": [ {\"offerType\" : \"Coupons\"}, {\"offerType\" : \"Cashback\"}, {\"offerType\" : \"Deals\"}, {\"offerType\" : \"Top Deals\"}]}";

List<String> offerTypesArrayList = new ArrayList<String>();

//convert json string into array list
try {
    JSONObject jObj = new JSONObject(offerJson);
    JSONArray offerTypes = jObj.getJSONArray("offerTypes");

    for (int i = 0; i < offerTypes.length(); i++) {
        JSONObject offertype = offerTypes.getJSONObject(i);

        String offerType = offertype.getString("offerType");
        offerTypesArrayList.add(offerType);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

//populate converted json data in spinner
spinnerOfferType = (Spinner) findViewById(R.id.spinnerOfferType);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, offerTypesArrayList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerOfferType.setAdapter(arrayAdapter);

Spinner Item Selected Listener

When an item is selected in spinner, spinner object receives item selected event. To handle item selected event, you need to define listener by implementing AdapterView.OnItemSelectedListener and set it to spinner object.

AdapterView.OnItemSelectedListener has two callback methods, onItemSelected and onNothingSelected. Method onItemSelected is called when an item is selected and method onNothingSelected is called when an item is unselected.

 
spinnerOfferType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
    public void onItemSelected(AdapterView parent, View view, int pos, long id) {
        String item = spinnerOfferType.getSelectedItem().toString();
        slectItem.setText(item);
    }

    public void onNothingSelected(AdapterView parent) {

    }
});
 

Spinner Mode

You can set spinner mode in xml and in code. In xml, you can set spinner mode using spinnerMode attribute and in code, you can pass spinner mode to one of the Spinner constructors.

Spinner mode setting defines how spinner items are shown when you click spinner to reveal spinner items. Spinner supports two modes, dialog (Spinner.MODE_DIALOG) and dropdown (Spinner.MODE_DROPDOWN) mode. By default, spinner items are shown in dropdown mode. Below screen shot show spinner in dialog mode.

android spinner dialog mode

Spinner Prompt

Below screen shot show spinner with prompt. You can set prompt in xml using prompt attribute and in code by calling setPrompt on spinner object. Spinner attribute prompt is applicable only when spinner is in dialog mode. Highlighted prompt with red rectangle below.

android spinner prompt

Spinner Popup Background

You can set background image for popup when spinner is in dialog mode. You can set back ground for popup by calling setPopupBackgroundResource method on spinner.

 spinnerOfferType.setPopupBackgroundResource(R.drawable.zoftino);
 
android spinner popup background

Spinner Style

To change the style of spinner, you need to create item layout and dropdown view and apply styles. In the above examples, we used android provided item layout android.R.layout.simple_spinner_item and dropdown view android.R.layout.simple_spinner_dropdown_item.

To show how to customize spinner style, we will create layouts similar to what we used and apply styles.

android spinner custom style

Item layout


Drop down view

 

Styles