Spinner - Android Studio - Kotlin
How to use the Spinner in Android Studio with Kotlin
In this tutorial, you’ll learn how to use a Spinner in Android Studio using Kotlin. A Spinner in Android works like a drop-down menu that displays a list of values, allowing the user to select one option at a time.
The Android Spinner is part of the AdapterView family, which means it requires an Adapter to bind data to the UI. In this example, we’ll use an adapter to populate the Spinner with items and handle user selection events.
Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:padding="10dp" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginVertical="40dp" android:text="Spinner" android:textAlignment="center" android:textSize="20sp" android:textStyle="bold" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/outputSpinnerTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#000" android:textSize="30sp" /> </LinearLayout>
MainActivity.kt
package com.technifysoft.myapplication import android.os.Bundle import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.Spinner import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivityKt : AppCompatActivity() { private lateinit var mSpinner: Spinner private lateinit var mOutputSpinnerTv: TextView //options to be displayed in spinner var mOptions: Array<String> = arrayOf("Canada", "Pakistan", "Turkey", "US") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //init UI views mSpinner = findViewById(R.id.spinner) mOutputSpinnerTv = findViewById(R.id.outputSpinnerTv) //Creating the ArrayAdapter instance having the list of options val arrayAdapter = ArrayAdapter<Any?>(this, android.R.layout.simple_spinner_item, mOptions) arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) //setting the ArrayAdapter data on the Spinner mSpinner.setAdapter(arrayAdapter) //spinner item click handler mSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected( parent: AdapterView<*>?, view: View?, position: Int, id: Long ) { //METHOD 1: Get text from selected item's position & set it to TextView //mOutputSpinnerTv.setText(parent.getItemAtPosition(position).toString()); //METHOD 2: Get the position of item selected, & perform specific task if (position == 0) { mOutputSpinnerTv.text = "Canada is selected..." } else if (position == 1) { mOutputSpinnerTv.text = "Pakistan is selected..." } else if (position == 2) { mOutputSpinnerTv.text = "Turkey is selected..." } else if (position == 3) { mOutputSpinnerTv.text = "US is selected..." } } override fun onNothingSelected(parent: AdapterView<*>?) { } } } }


Comments
Post a Comment