Sunday, 25 March 2018

PopUp Menu Using Kotlin in Android



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 have one XML file activity_main.xml.In that we take one Button In side Relative layout.

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:id="@+id/relativeLayout"
    tools:context="com.dharmendra.popupmenu.MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Click" />
</RelativeLayout>

3.We need to create one more xml file inside res->menu folder.
First we have to Create folder name menu inside res folder. And inside menu folder we have to create xml file name popup_menu.xml


Right Click on menu folder. New->menu resource file provide name to file and click Ok.
popup_menu.xml file contain options which we want to provide. 

popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/red"
        android:title="Red" />

    <item
        android:id="@+id/blue"
        android:title="Blue" />

    <item
        android:id="@+id/green"
        android:title="Green" />


</menu>


4.MainActivity.kt


package com.dharmendra.popupmenu

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.PopupMenu
import android.widget.RelativeLayout

class MainActivity : AppCompatActivity() {

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

        //Find View By Id for button
        val btn = findViewById<Button>(R.id.button) as Button

        //Find View By Id for Relative Layout because We need to Change It's Color.
        val background = findViewById<RelativeLayout>(R.id.relativeLayout) as RelativeLayout

        //On Click for Button to open PopUp menu
        btn.setOnClickListener {

            /*Create Object Of PopupMenu
             we need to pas  Context and View (Our View is Button so we pass btn to it)
             */

            val popUp = PopupMenu(this@MainActivity, btn)

            //Inflate our menu Layout.
            popUp.menuInflater.inflate(R.menu.popup_menu, popUp.menu)


            //Set Click Listener on Popup Menu Item
            popUp.setOnMenuItemClickListener { myItem ->

                //Getting Id of selected Item
                val item = myItem!!.itemId

                when (item) {
                    R.id.red -> {
                        background.setBackgroundColor(Color.RED)
                    }

                    R.id.blue -> {
                        background.setBackgroundColor(Color.BLUE)
                    }

                    R.id.green -> {
                        background.setBackgroundColor(Color.GREEN)
                    }
                }

                true
            }
            popUp.show()


        }
    }
}

Output:



No comments:

Post a Comment