Open In App
Related Articles

Dynamic Spinner in Kotlin

Improve Article
Save Article
Like Article

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

Default value of the android spinner will be currently selected value and by using Adapter we can easily bind the items to spinner object.
Here, we will create the spinner programmatically in Kotlin file.

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 next button.
  4. Then select the Empty activity => next => finish.

Modify activity_main.xml file

In this file, we use the TextView widget and also set its attributes.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:gravity = "center">
        android:text="Select language:"
        android:textSize = "20dp" />

Update strings.xml file

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


    <string name="app_name">SpinnerInKotlin</string>
    <string name="selected_item">Selected item:</string>
    <string-array name="Languages">

Create Spinner in MainActivity.kt file

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

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

then, we can create the spinner using

val spinner = Spinner(this)
    spinner.layoutParams = LinearLayout.LayoutParams(

Add the spinner in the linear Layout using

val linearLayout = findViewById<LinearLayout>(
        //add spinner in linear layout


package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // access the items of the list
        val languages = resources.getStringArray(R.array.Languages)
        //create spinner programmatically
        val spinner = Spinner(this)
        spinner.layoutParams = LinearLayout.LayoutParams(
        val linearLayout = findViewById<LinearLayout>(
        //add spinner in linear layout
        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) {
                    getString(R.string.selected_item) + " " +
                         "" + languages[position], Toast.LENGTH_SHORT).show()
                override fun onNothingSelected(parent: AdapterView<*>) {
                    // write code to perform some action

AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Run as Emulator:


Last Updated : 28 Mar, 2022
Like Article
Save Article
Similar Reads