Android Spinner and its item selection

In Android, Spinner is like a dropdown list. It displays a list of items. One of its items can be selected at a time. You can use an array or an ArrayList object that contains items as its data source. Typically the array or ArrayList object is not directly set to the Spinner. You need to create an object of ArrayAdapter and pass the array or ArrayList object to its constructor. Then use the setAdapter method to set the ArrayAdapter object to the Spinner.

Now to be clear, you need to create a new Android project. Then edit the activity_main.xml file to add a Spinner as shown below.

<LinearLayout xmlns:android=""
   tools:context=".MainActivity" >


The Spinner needs its own layout file that defines its item template. In the layout file, you need to have one TextView. When you attach a data source to the Spinner, Android looks for the TextView in the layout file to display its text items. Here is the layout file of the Spinner (spinner_layout.xml).

<?xml version="1.0" encoding="utf-8"?>

<!-- Single Item Design -->
<RelativeLayout xmlns:android=""


Double-click the on the left side to open the MainAcitivity class. Then in the onCreate() method to the MainActivity class, you need to write code to make a reference to the Spinner, create an array of strings to use as data source of the Spinner, create an object of ArrayAdapter, and use the setAdapter method to attach the data source to the Spinner. When you create the ArrayAdapter object, you need to pass four values: current activity, a resource layout file that contains a TextView, the TextView resource id to display items of the spinner, and the data source.
To respond to user selections, you need to register the Spinner to receive the item selection event by using the setOnItemSelectedListener() method. You pass to this method an object of a class that implements the OnItemSelectedListener interface. The onItemSelected() method to perform action when an item is selected.

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
 protected void onCreate(Bundle savedInstanceState) {
   Spinner sp=(Spinner)findViewById(;
   String[] items={"Cambodia","Japan","USA"};
   ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.spinner_layout,, items);
   sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    ViewGroup vg=(ViewGroup)view;
   TextView tv=(TextView)vg.findViewById(;
   Toast.makeText(MainActivity.this, tv.getText().toString(), Toast.LENGTH_LONG).show();
   public void onNothingSelected(AdapterView<?> parent) {



When you run the project again and select an item of the Spinner, you will see a Toast message showing the selected item.


Posted by: Dara | post date: 05-25-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.