Sunday, 11 March 2018

Android Spinner/Drop Down Menu Example Using Kotlin


1. Creating New Project in Kotlin
  • Open Android Studio.
  • Go to File => New => New Project.Give Name To your application. Then, check Include Kotlin Support and click next button.
  • Select minimum SDK you need. However, we have selected 21 as minimum SDK. Then, click next button.
  • Then, select Empty Activity => click next => click finish.
2. Here We Take Spinner in our Layout . As in below xml layout.
main_activity.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"
    tools:context="com.dharmendra.spinner.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="73dp"
        android:gravity="center"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark"
        android:text="Select Country"
        android:id="@+id/textView" />

    <Spinner
        android:id="@+id/country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginStart="16dp"
        android:layout_marginTop="26dp" />


</RelativeLayout>


3. MainActivity.kt

package com.dharmendra.spinner

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*

class MainActivity : AppCompatActivity() {

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

        //Find View by id for Spinner and Button


        val spinner=findViewById<Spinner>(R.id.country) as Spinner

        //Create An Array which Contain Country name

        val country= arrayOf("India","United States","Canada","Russia","Brazil")

        //Set Array in Adapter

        val adapter=ArrayAdapter(this@MainActivity,android.R.layout.simple_spinner_dropdown_item,country)

        spinner.adapter=adapter


      spinner.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
          override fun onNothingSelected(p0: AdapterView<*>?) {
              TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
          }

          override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {

              Toast.makeText(this@MainActivity,"You have Selected "+country[p2],Toast.LENGTH_LONG).show()

          }

      }



    }
}

Output:

No comments:

Post a Comment