Spinner | Android Studio | Kotlin

Spinner Example | Android Studio |  Kotlin

In this tutorial, we will learn how to use Spinner.
Android spinner is like the drop-down menu with multiple values from which the end-user can select only one value. Android spinner is associated with AdapterView. So you need to use one of the adapter classes with spinner.
We will select an item from the Spinner and set it to the TextView.
>>Watch For Java
Spinner Example Android Studio -  Kotlin

Step 1: Create a new Project or open a new project

Step 2: Code

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="5dp"
    tools:context=".MainActivity">

    <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_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Hello World!"
        android:textColor="#000"
        android:textSize="30sp" />

</RelativeLayout>

MainActivity.kt

package com.blogspot.atifsoftwares.myapplication

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    //options to be displayed in spinner
    var mOptions = arrayOf("Canada", "Pakistan", "Turkey", "US")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Creating the ArrayAdapter instance having the list of options
        val aa: ArrayAdapter<String> =
            ArrayAdapter(this, android.R.layout.simple_spinner_item, mOptions)
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        //setting the ArrayAdapter data on the Spinner
        spinner!!.adapter = aa


        //spinner item click handler
        spinner!!.onItemSelectedListener = object : 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) {
                    outputSpinnerTv!!.text = "Canada is selected..."
                }
                if (position == 1) {
                    outputSpinnerTv!!.text = "Pakistan is selected..."
                }
                if (position == 2) {
                    outputSpinnerTv!!.text = "Turkey is selected..."
                }
                if (position == 3) {
                    outputSpinnerTv!!.text = "US is selected..."
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        }

    }
}

Step 3: Run Project

Output
Spinner Example Android Studio -  Kotlin


Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)