AppCompatSpinner

AppCompatSpinner allows you to select an item from a drop down menu. AppCompatSpinner provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value.

Class Hierarchy
public class AppCompatSpinner extends Spinner implements TintableBackgroundView
java.lang.Object
↳ android.view.View
  ↳ android.view.ViewGroup
    ↳ android.widget.AdapterView<android.widget.SpinnerAdapter>
      ↳ android.widget.AbsSpinner
        ↳ android.widget.Spinner
          ↳ android.support.v7.widget.AppCompatSpinner

We Recommend to use new UI components i.e. androidx components


It can be used to display the multiple options to the user in which only one item can be selected by the user.

Android spinner is like the drop down menu with multiple values from which the end user can select only one value.

Android spinner is associated with AdapterView. So you need to use one of the adapter classes with spinner.

Android Spinner Example

In this example, we are going to display the fruits list. You need to use ArrayAdapter class to store the fruits list.
Let's see the simple example of spinner in android.

spinner_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatSpinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/showSelection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show My Choice"
        android:onClick="yourChoice"/>
</androidx.appcompat.widget.LinearLayoutCompat>

SpinnerActivity.java
package com.theitbulls.uicomponentex;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;

public class SpinnerActivity extends AppCompatActivity {
    private AppCompatSpinner spinner;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_activity);

        String[] fruits = getResources().getStringArray(R.array.fruits);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, fruits);
        spinner = findViewById(R.id.spinner);
        spinner.setAdapter(adapter);
    }

    public void yourChoice(View view) {
        String selectedItem  = spinner.getSelectedItem().toString();
        Toast.makeText(this, "Your selected fruits: " + selectedItem, Toast.LENGTH_SHORT).show();
    }
}


strings.xml
<string-array name="fruits">
    <item>Apple</item>
    <item>Orange</item>
    <item>Mango</item>
    <item>Grapes</item>
</string-array>