Dynamic Spinner in Kotlin
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:
- Click on File, then New => New Project.
- 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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear_layout"
android:gravity = "center">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select language:"
android:textSize = "20dp" />
</LinearLayout>
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:
<resources>
<string name="app_name">SpinnerInKotlin</string>
<!-- add the following -->
<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 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:
package org.geeksforgeeks.demo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.*
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)
//create spinner programmatically
val spinner = Spinner(this)
spinner.layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
val linearLayout = findViewById<LinearLayout>(R.id.linear_layout)
//add spinner in linear layout
linearLayout?.addView(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, getString(R.string.selected_item) + " " +"" + languages[position], Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent: AdapterView<*>) {
// write code to perform some action
}
}
}
}
}