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="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/static_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginTop="20dp" /> <Spinner android:id="@+id/dynamic_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Add a list of options to your string resource file.
File: strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Ahotbrew.com - Dropdown</string> <string-array name="brew_array"> <item>Cappuccino</item> <item>Espresso</item> <item>Mocha</item> <item>Caffè Americano</item> <item>Cafe Zorro</item> </string-array> </resources>
Add the following code to your activity inside the onCreate method.
File: MainActivity.java
package com.ahotbrew.androiddropdown; import android.app.Activity; 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner); // Create an ArrayAdapter using the string array and a default spinner ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter .createFromResource(this, R.array.brew_array, android.R.layout.simple_spinner_item); // Specify the layout to use when the list of choices appears staticAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner staticSpinner.setAdapter(staticAdapter); Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner); 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.setAdapter(adapter); dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.v("item", (String) parent.getItemAtPosition(position)); } @Override 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.