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/Java file.
Important Attributes for Spinner Widget
| XML attributes | Description |
|---|---|
| android:id | Used to specify the id of the view. |
| android:textAlignment | Used to the text alignment in the dropdown list. |
| android:background | Used to set the background of the view. |
| android:padding | Used to set the padding of the view. |
| android:visibility | Used to set the visibility of the view. |
| android:gravity | Used to specify the gravity of the view like center, top, bottom, etc |
Steps to Implement Spinner
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.
Step 1: Create a new Project
To create a new Project in Android Studio please refer to How to Create/Start a New Project in Android Studio using Kotlin.
We are going to use both Java and Kotlin.
Step 2: Working with the activity_main.xml
Now open activity_main.xml and insert the below code in it.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--Constraint layout which contain Spinner widget-->
<androidx.constraintlayout.widget.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"
android:background="@color/white"
tools:context=".MainActivity">
<!--Spinner widget-->
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingHorizontal="16dp"
android:paddingVertical="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Working with the MainActivity.kt
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:
MainActivity File:
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
// Create array of Strings and store the names of courses
private 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
Spinner spin = findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
// Create the instance of ArrayAdapter having the list of courses
ArrayAdapter<String> 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
spin.setAdapter(ad);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Make toast of the name of the course which is selected in the spinner
Toast.makeText(getApplicationContext(), courses[position], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// No action needed when no selection is made
}
}
package org.geeksforgeeks.demo
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
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(), OnItemSelectedListener {
// create array of Strings
// and store name of courses
private var courses = arrayOf(
"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.spinner)
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 toast of name of course
// which is selected in spinner
Toast.makeText(applicationContext, courses[position], Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}