VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/dynamic-spinner-in-kotlin/

⇱ Dynamic Spinner in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dynamic Spinner in Kotlin

Last Updated : 12 Jul, 2025

The Android Spinner widget is a UI component that is used to select from a drop down list. When the user taps the Spinner, it displays a dropdown list containing all available items. Upon selection, the Spinner updates to show the chosen value as the default. To provide a Spinner with data, we use an Adapter, which efficiently binds a list of items to the Spinner widget. In this article, we will demonstrate how to programmatically create a Dynamic Spinner in the Kotlin language without setting up in the layout.

Steps Of Implementing Dynamic Spinner:

Step 1: Create New Project in Android

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.

Note: To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Select Java or Kotlin as the programming language.

Step 2: Working with activity_main.xml

activity_main.xml:

Layout:

👁 Layout


Step 3: 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.

strings.xml:


Step 3: Create 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 can create the spinner using the following code

val spinner = Spinner(this)
spinner.layoutParams = LinearLayout.LayoutParams(
 ViewGroup.LayoutParams.WRAP_CONTENT,
 ViewGroup.LayoutParams.WRAP_CONTENT
)

Add the spinner in the linear Layout using

val linearLayout :LinearLayout = findViewById(R.id.linear_layout)
linearLayout?.addView(spinner)

MainActivity.kt:

Output:

Comment
Article Tags:

Explore