

Import Ĭlass MainActivity : AppCompatActivity(), AdapterView. Step 4 − Add the following code to MainActivity.kt import android.os.Bundle Step 3 − Open res/strings.xml and add the following code − Step 2 − Add the following code to res/layout/activity_main.xml.

Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Create a single MainActivity that contains the spinner and on clicking any item of spinner Toast with that course name will be shown.This example demonstrates how to get Spinner value in Kotlin. Use ArrayAdapter to store the courses list. Here is an example of an Android application that displays the list of courses of GFG.

Used to specify the gravity of the view like center, top, bottom, etc

Used to the text alignment in the dropdown list. Different Attributes for Spinner Widget XML attributes Generally, we populate our Spinner control with a list of items by using an ArrayAdapter in our Kotlin file. In this tutorial, you'll learn how to get the value of the Selected Item from your Spinner (time 0:45). Now after that we are adding setOnClickListener method on button and inside that button we are. Now we are adding setOnItemSelectedListener function on spinner to get selected item of spinner and transfer the spinner selected item value to string variable. The default value of the android spinner will be the currently selected value and by using Adapter we can easily bind the items to the spinner objects. In this tutorial we are creating a simple spinner with three text items values. 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.
