This example creates a spinner which is also known as a drop-down list. A spinner enables a user to select a single value.
Main Steps
1) Create an activity that implements the OnItemSelectedListener interface.
2) In res/values, create a string array of values for your spinner.
Example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Desserts">
<item>Default Dessert</item>
<item>Buttermilk Doughnuts</item>
<item>Chocolate Chip Cookies</item>
<item>Ginger Cupcakes</item>
<item>Mint Truffles</item>
<item>Oatmeal Cookies</item>
</string-array>
</resources>
3) Add a spinner to your layout XML.
Example:
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/teal"
android:focusable="true"
android:prompt="@string/spinnerPrompt"
android:spinnerMode="dropdown"
android:focusableInTouchMode="true" />
4) Register the spinner.
sp = (Spinner) findViewById(R.id.spinner1);
5) Create an adapter that pulls the string array data from resources.
6) Select a row layout.
7) Connect the adapter to your spinner.
sp.setAdapter(adapter);
8) Connect the item listener to the spinner.
9) Implement two methods required by the OnItemSelectedListener interface:
A) public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
In this code example, the Toast gets the string value of the Spinner item selected using this statement:
str = parent.getItemAtPosition(pos).toString();
B) public void onNothingSelected(AdapterView<?> arg0)
Official documentation for those methods
http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html
abstract void onItemSelected(AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this view has been selected.
Parameters
parent The AdapterView where the selection happened
view The view within the AdapterView that was clicked
position The position of the view in the adapter
id The row id of the item that is selected
abstract void onNothingSelected(AdapterView<?> parent)
Callback method to be invoked when the selection disappears from this view.
My code example of a spinner
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainSpinnerActivity extends Activity implements
OnItemSelectedListener {
Spinner sp;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Register spinner.
sp = (Spinner) findViewById(R.id.spinner1);
// Create an adapter that pulls the string array data from resources.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Desserts,
android.R.layout.simple_spinner_dropdown_item);
// Select row layout.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Connect the adapter to your spinner.
sp.setAdapter(adapter);
// Connect the item listener to the spinner.
sp.setOnItemSelectedListener(this);
}
// onItemSelected() and onNothingSelected() are required to implement the
// OnItemSelectedListener interface.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
str = parent.getItemAtPosition(pos).toString();
Toast.makeText(MainSpinnerActivity.this, str, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// If nothing is selected.
}
}