By Barry A. Burd

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.

java-programming-for-android-developers-2e-textview
A TextView component and a spinner.

Let’s expand the choices.

java-programming-for-android-developers-2e-spinner-choices
The user expands the spinner’s choices.

How about Bach?

java-programming-for-android-developers-2e-select-bach
The user has selected Bach.

Here’s the code.

package com.allmycode.a12_11;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

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;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Spinner spinner = (Spinner) findViewById(R.id.spinner);

textView = (TextView) findViewById(R.id.textView);

String[] choices =

{"Select a composer",

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

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

ArrayAdapter<String> adapter =

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

spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new MyItemSelectedListener());

}

class MyItemSelectedListener implements OnItemSelectedListener {

@Override

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

int position, long id) {

if (position == 0) {

textView.setText("You haven't selected a composer.");

} else {

textView.setText(adapterView.getItemAtPosition(position).toString());

}

}

@Override

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:

// Don't do this:

spinner.addRow("Monteverdi");

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.”

java-programming-for-android-developers-2e-spinner
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.