Android Drop Down List Example

Android dropdown example

In Android a Spinner renders as a dropdown. This tutorial will show you how to create an android  spinner dropdown from static and dynamic data and how to invoke OnItemSelectedListener after selecting an item.

Create Layout

Create your main layout file activity_main.xml. There are two spinners below. static_spinner will load the data from your strings.xml file and the dynamic_spinner will load the data from an array.

File: activity_main.xml

<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:layout_marginTop="20dp" />

        android:layout_height="wrap_content" />


Add a list of options to your string resource file.

File: strings.xml

<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name"> - Dropdown</string>

    <string-array name="brew_array">
        <item>Caffè Americano</item>
        <item>Cafe Zorro</item>


Add the following code to your activity inside the onCreate method.


package com.ahotbrew.androiddropdown; 

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {

		Spinner staticSpinner = (Spinner) findViewById(;

		// Create an ArrayAdapter using the string array and a default spinner
		ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
				.createFromResource(this, R.array.brew_array,

		// Specify the layout to use when the list of choices appears

		// Apply the adapter to the spinner

		Spinner dynamicSpinner = (Spinner) findViewById(;

		String[] items = new String[] { "Chai Latte", "Green Tea", "Black Tea" };

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, items);


		dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Log.v("item", (String) parent.getItemAtPosition(position));

			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

You’ll notice that the first dropdown list is more spaced out than our second dropdown. This was accomplished  by specifying a layout for the adapter as you can see on line 27.

The dynamic spinner has an OnItemSelectListener attached to it, where the method onItemSelected is called every time an item is selected.

Sit back, take a sip from your hot brew and run your project.

Don’t forget to join or newsletter and get free android tutorials to enjoy with your hot brew.

  • adhieresthenes

    What means this method from? -> “onNothingSelected” I cant use it to define something. Would you clarify that case. Thanks

  • Gurinder

    onNothingSelected is a Callback method to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter becomes empty.

    You can find more information on google

  • don k4sperski

    when using fragments change

    ArrayAdapter adapter = new ArrayAdapter(this,

    android.R.layout.simple_spinner_item, items);


    ArrayAdapter adapter = new ArrayAdapter(getActivity(),

    android.R.layout.simple_spinner_item, items);

    so you can get context