Java Programming for Android Developers For Dummies
Book image
Explore Book Buy On Amazon
If you look at the Palette in Android Studio's Designer tool, you can find the Spinner component. You can drag a Spinner component from the Palette onto one of your app's preview screens. A Spinner component is a drop-down list — a bunch of alternatives for the user to choose from. That “bunch” of alternatives is a collection of some sort. Here, you use an array to implement the collection.

A TextView component and a spinner.

Let’s expand the choices.

The user expands the spinner's choices.

How about Bach?

The user has selected Bach.

Here’s the code.

package com.allmycode.a12_11;

import android.os.Bundle;


import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView textView;


protected void onCreate(Bundle savedInstanceState) {



Spinner spinner = (Spinner) findViewById(;

textView = (TextView) findViewById(;

String[] choices =

{"Select a composer",

"Monteverdi", "Pachelbel", "Corelli", "Albinoni",

"Vivaldi", "Telemann", "Handel","Bach", "Scarlatti"};

<strong> ArrayAdapter<String> adapter =</strong>

<strong> new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, choices);</strong>

<strong> </strong>

<strong> spinner.setAdapter(adapter);</strong>

<strong> spinner.setOnItemSelectedListener(new MyItemSelectedListener());</strong>


<strong> class MyItemSelectedListener implements OnItemSelectedListener {</strong>

<strong> </strong>

<strong> @Override</strong>

<strong> public void onItemSelected(AdapterView<?> adapterView, View view,</strong>

<strong> int position, long id) {</strong>

<strong> </strong>

<strong> if (position == 0) {</strong>

<strong> textView.setText("You haven't selected a composer.");</strong>

<strong> } else {</strong>

<strong> textView.setText(adapterView.getItemAtPosition(position).toString());</strong>

<strong> }</strong>

<strong> }</strong>


public void onNothingSelected(AdapterView<?> adapterView) {

// Do nothing




To make a spinner do its job, you create a listener and an adapter.

The listener

A spinner's listener is much like a button's listener. It's a piece of code that listens for user actions and responds when an appropriate action occurs.

In the code above, a listener is created (an instance of MyItemSelectedListener class). You tell Android to notify the listener when the user selects one of the spinner's items:

spinner.setOnItemSelectedListener(new MyItemSelectedListener());

The MyItemSelectedListener class's onItemSelected method must tell Android what to do in response to the user's selection.

The adapter

You may guess that you add an item to a spinner with a call like this:

<strong>// Don't do this:</strong>


But that's not the way it works. When an Android developer thinks about a spinner, the developer thinks about two different concepts:

  • A spinner has data.

The spinner's data consists of the values "Select a composer", "Monteverdi", "Pachelbel", and so on.

  • A spinner has a “look.”

This section's spinner has a simple look. In the first image, the spinner has text on the left side and a tiny downward arrow on the right side. In the second image, each of the spinner's items has text on the left side.

A spinner's incarnation on the screen (the “look”) is an object in and of itself. It's an instance of Android's AdapterView class. A similar-sounding thing, an instance of the SpinnerAdapter class, connects a spinner's data with a spinner's “look.”

How a spinner works.

There are several kinds of spinner adapter, including the ArrayAdapter and CursorAdapter classes:

  • An ArrayAdapter gets data from a collection, such as an array or an ArrayList.
  • A CursorAdapter gets data from a database query.
In the code above, an ArrayAdapter is used. The ArrayAdapter constructor has three parameters:
  • The first parameter is a context.

Use this for the context. The word this represents whatever object contains the current line of code. In the code, this refers to the MainActivity.

  • The second parameter is a layout.

The name android.R.layout.simple_spinner_item refers to a standard layout for one of the items.

  • The third parameter is the source of the data.

You can provide choices, which you declare to be an array of String values.

Notice the onItemSelected method's position parameter. When the user selects the topmost item in the spinner's list (the Select a Composer item), Android gives that position parameter the value 0. When the user selects the next-to-topmost item (the Monteverdi item), Android gives that position parameter the value 1. And so on.

In the onItemSelected method's body, the code checks to make sure that position isn't 0. If position isn't 0, the code plugs that position value into the adapterView.getItemAtPosition method to get the string on whatever item the user clicked. The code displays that string (Monteverdi, Pachelbel, or whichever) in a textView component.

About This Article

This article is from the book:

About the book author:

Barry Burd, PhD, is a professor in the Department of Mathematics and Computer Science at Drew University in Madison, New Jersey. He has lectured at conferences in the United States, Europe, Australia, and Asia. He hosts podcasts and videos about software and other technology topics. He is the author of many articles and books, including Java For Dummies.

This article can be found in the category: