Spinner in Android with Example - GeeksforGeeks

17 Aug, 2021
Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the currently selected value and by using Adapter we can easily bind the items to the spinner objects. Generally, we populate our Spinner control with a list of items by using an ArrayAdapter in our Kotlin file. 
Here is an example of an Android application that displays the list of courses of GFG. Use ArrayAdapter to store the courses list. Create a single MainActivity that contains the spinner and on clicking any item of spinner Toast with that course name will be shown. 
Creating the activities: There will be one activity and hence one XML file for MainActivity. activity_main.xml: XML file for first activity consists of constraint layout with spinner widget. Below is the code for the XML file for activity:
XML
<?xml version="1.0" encoding="utf-8"?><!--Constraint layout which contain Spinner widget--> <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="com.geeksforgeeks.Spinner.MainActivity">     <!--Spinner widget-->    <Spinner        android:id="@+id/coursesspinner"        android:layout_height="50dp"        android:layout_width="160dp"        android:layout_marginEnd="10dp"        android:layout_marginStart="10dp"           android:layout_marginBottom="10dp"        android:layout_marginTop="10dp"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"/>   </android.support.constraint.ConstraintLayout>
Creating the Java and Kotlin file: There is one activity and hence one Java/Kotlin file for the MainActivity file. Java/Kotlin file for Main Activity, in which Array Adapter is used to bind data to the spinner. We will fill data in the array of strings and bind that data to the spinner. Here is the code:  
Java
Kotlin
import android.support.v7.app.AppCompatActivity;import android.widget.AdapterView;import android.view.View;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.os.Bundle; // Main Activity implements Adapter viewpublic class MainActivity    extends AppCompatActivity    implements AdapterView.OnItemSelectedListener {     // create array of Strings    // and store name of courses    String[] courses = { "C", "Data structures",                        "Interview prep", "Algorithms",                        "DSA with java", "OS" };     @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         // Take the instance of Spinner and        // apply OnItemSelectedListener on it which        // tells which item of spinner is clicked        Spinner spino = findViewById(R.id.coursesspinner);        spin.setOnItemSelectedListener(this);         // Create the instance of ArrayAdapter        // having the list of courses        ArrayAdapter ad            = new ArrayAdapter(                this,                android.R.layout.simple_spinner_item,                courses);         // set simple layout resource file        // for each item of spinner        ad.setDropDownViewResource(            android.R.layout                .simple_spinner_dropdown_item);         // Set the ArrayAdapter (ad) data on the        // Spinner which binds data to spinner        spino.setAdapter(ad);    }     // Performing action when ItemSelected    // from spinner, Overriding onItemSelected method    @Override    public void onItemSelected(AdapterView<*> arg0,                            View arg1,                            int position,                            long id)    {         // make toastof name of course        // which is selected in spinner        Toast.makeText(getApplicationContext(),                    courses[position],                    Toast.LENGTH_LONG)            .show();    }     @Override    public void onNothingSelected(AdapterView<*> arg0)    {        // Auto-generated method stub    }}
import android.os.Bundleimport android.view.Viewimport android.widget.AdapterViewimport android.widget.AdapterView.OnItemSelectedListenerimport android.widget.ArrayAdapterimport android.widget.Spinnerimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity(), OnItemSelectedListener {    // create array of Strings    // and store name of courses    var courses = arrayOf<String?>("C", "Data structures",            "Interview prep", "Algorithms",            "DSA with java", "OS")     override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)         // Take the instance of Spinner and        // apply OnItemSelectedListener on it which        // tells which item of spinner is clicked        val spin = findViewById<Spinner>(R.id.coursesspinner)        spin.onItemSelectedListener = this         // Create the instance of ArrayAdapter        // having the list of courses        val ad: ArrayAdapter<*> = ArrayAdapter<Any?>(                this,                android.R.layout.simple_spinner_item,                courses)         // set simple layout resource file        // for each item of spinner        ad.setDropDownViewResource(                android.R.layout.simple_spinner_dropdown_item)         // Set the ArrayAdapter (ad) data on the        // Spinner which binds data to spinner        spin.adapter = ad    }     override fun onItemSelected(parent: AdapterView<*>?,                                view: View, position: Int,                                id: Long) {        // make toastof name of course        // which is selected in spinner        Toast.makeText(applicationContext,                courses[position],                Toast.LENGTH_LONG)                .show()    }     override fun onNothingSelected(parent: AdapterView<*>?) {}}
AmiyaRanjanRout
anikaseth98
android
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Multidimensional Arrays in Java
Overriding in Java
Stack Class in Java
Collections in Java
Set in Java
Singleton Class in Java
LinkedList in Java