Android Development: Basic Control Spinner Dropdown List Control


Spinner provides a quick way to select a value from a data set. By default, Spinner displays the currently selected value. Clicking on Spinner will bring up a drop down menu containing all the optional values from which a new value can be selected for Spinner.

The figure above shows the common style of Spinner. In this article I will discuss 1.Spinner's basic usage 2. Setting Spinner's Adapter (arrayadapter and custom BaseAdapter)3.Spinner's menu display 4.Spinner's xml properties

The Simplest Spinner
Adding Spinner Control to Layout File

        android:orientation="vertical" >

Android:entries="@array/languages" indicates that Spinner's data set is obtained from the resource array languages, which is defined in values/arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
    <string-array name="languages">
        <item>c language</item>
        <item>java </item>

If you don't need to respond to Spinner's selection events, a complete Spinner usage process is over.

Of course, in general, we need to respond to Spinner selection events, which can be achieved through OnItemSelectedListener's callback method.

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        Spinner spinner = (Spinner) findViewById(;
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {
                String[] languages = getResources().getStringArray(R.array.languages);
                Toast.makeText(MainActivity.this, "What you clicked on was:"+languages[pos], 2000).show();
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback


Link to the original text:

Keywords: Android xml encoding Java

Added by ccbayer on Sun, 06 Oct 2019 21:33:49 +0300