Open In App

Spinner in Kotlin

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Android Spinner is a view similar to a 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 the 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.

Some Important Attributes of 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

Step 1: Create a new Project

First, we create a new project by following the below steps:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click the Next button.
  4. Then select the Empty activity => Next => Finish.

To create a new Project in Android Studio please refer to How to Create/Start a New Project in Android Studio using Kotlin.

Step 2: Modifying the activity_main.xml

Now open activity_main.xml and insert the below code in it. In this file, we use the TextView and Spinner widgets and also set their attributes.

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@color/white"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select language:"
        android:textSize="20sp" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Layout:

Layout


Step 3: Update strings.xml file

Here, we update the name of the application using the string tag. We also create a list of the items which will be used in the dropdown menu.

strings.xml:

XML
<resources>
  
  	<!-- add the following below to the existing code-->
    <string name="selected_item">Selected item:</string>
    <string-array name="Languages">
        <item>Java</item>
        <item>Kotlin</item>
        <item>Swift</item>
        <item>Python</item>
        <item>Scala</item>
        <item>Perl</item>
    </string-array>
</resources>


Step 4: Access Spinner in MainActivity.kt file

First, we declare a variable languages to access the strings items from the strings.xml file.

val languages = resources.getStringArray(R.array.Languages)

then, we access the spinner and set ArrayAdaptor to control the list of items.

Kotlin
val spinner = findViewById(R.id.spinner)
 if (spinner != null) {
    val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, languages)
    spinner.adapter = adapter
    
    // rest of the code
}

MainActivity.kt:

Kotlin
package org.geeksforgeeks.demo

import androidx.appcompat.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.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // access the items of the list
        val languages = resources.getStringArray(R.array.Languages)

        // access the spinner
        val spinner = findViewById<Spinner>(R.id.spinner)
        if (spinner != null) {
            val adapter = ArrayAdapter(this,
                android.R.layout.simple_spinner_item, languages)
                
            spinner.adapter = adapter

            spinner.onItemSelectedListener = object :
                AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parent: AdapterView<*>,
                                            view: View, position: Int, id: Long) {
                    Toast.makeText(this@MainActivity, getString(R.string.selected_item) + " " +
                                "" + languages[position], Toast.LENGTH_SHORT).show()
                }

                override fun onNothingSelected(parent: AdapterView<*>) {
                    // write code to perform some action
                }
            }
        }
    }
}

Output:


Next Article
Article Tags :

Similar Reads