Lesson 56. Spinner - drop-down list

Lesson 56. Spinner – drop-down list

In this lesson:

– we use Spinner

Too many lessons have been learned about adapters and lists. At first I wanted to show just a couple of examples, but I almost pulled all the existing adapters and painted them) Finally we will make a couple of useful examples where the adapters are still used. In this little lesson, let’s look at Spinner.

Spinner is a drop-down list that allows you to select a single value. It saves space on screen. I think everyone has met him not only on Android but in other programming languages ​​as well.

Let’s make a simple example.

Let’s create a project:

Project name: P0561_Spinner
Build Target: Android 2.3.3
Application name: Spinner
Package name: ru.startandroid.develop.p0561spinner
Create Activity: MainActivity

In the screen main.xml let’s put Spinner:


code MainActivity.java:

package ru.startandroid.develop.p0561spinner;

import android.app.Activity;
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.Toast;

public class MainActivity extends Activity {
  String[] data = {"one", "two", "three", "four", "five"};
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        // адаптер
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, data);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        // заголовок
        // выделяем элемент 
        // устанавливаем обработчик нажатия
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
      public void onItemSelected(AdapterView parent, View view,
          int position, long id) {
        // показываем позиция нажатого элемента
        Toast.makeText(getBaseContext(), "Position = " + position, Toast.LENGTH_SHORT).show();
      public void onNothingSelected(AdapterView arg0) {

The code is simple. We create an adapter, we use it simple_spinner_item as layout to display Spinner on screen. And with the setDropDownViewResource method, we specify which layout to use to draw list items.

The setPrompt method sets the text of the list header, and setSelection is the item we want to highlight. Both methods are, of course, optional. I used them for demonstration.

The list item handler is assigned by the setOnItemSelectedListener method. We will display the position of the selected element.

We’ll save everything and run it. Click on Spinner.

You can see the Title header and the highlighted item with position 2. Select item four.

The handler showed that they selected the item with position 3.

Use spinner.getSelectedItemPosition () to determine which item is represented now. To get the text of this element, you can immediately execute spinner.getSelectedItem (). ToString ().

In the next lesson:

– we use GridView

Discuss in the forum [102 replies]

Leave a Comment