Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
- Create the project and modify the main activity layout to add the spinner control.
12345678910111213141516171819202122232425262728293031323334<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Spinnerandroid:id="@+id/spinnerPlanet"android:layout_width="368dp"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="24dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/txtPlanet"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="65dp"android:gravity="center_horizontal"android:text="TextView"android:textSize="24sp"android:textStyle="bold"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/spinnerPlanet" /></android.support.constraint.ConstraintLayout>
- Modify the string values xml file add the spinner string array values.
123456789101112131415<resources><string name="app_name">HadSimpleSpinner</string><string-array name="planets_array"><item>Mercury</item><item>Venus</item><item>Earth</item><item>Mars</item><item>Jupiter</item><item>Saturn</item><item>Uranus</item><item>Neptune</item></string-array></resources>
- Modify the activity, create the spinner adapter and attach to the spinner control
123456789101112131415161718192021222324252627282930313233343536373839404142434445package com.dhamen.hadsimplespinner;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {TextView textViewPlanet;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textViewPlanet = findViewById(R.id.txtPlanet);Spinner spinner = findViewById(R.id.spinnerPlanet);spinner.setOnItemSelectedListener(this);ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.planets_array, android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner.setAdapter(adapter);}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {textViewPlanet.setText(parent.getItemAtPosition(position).toString());}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}}
- Watch the Video tutorial