Android Popup menu In Kotlin

 A PopupMenu displays is a floating menu , which is attached to a view The popup will appear below the anchor view . The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.


Project Detail
Project Name AndroidPopUpMenu
Package com.tutorialsbuzz.androidpopupmenu
Min Sdk Version 22
Target Sdk Version 29
Compile Sdk Version 29
Theme Theme.AppCompat.Light.DarkActionBar

1. Menu XML Layout







Inside the res/menu/ create a xml file "popup_menu.xml" .

file : res / menu /popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context="com.tutorialsbuzz.androidoptionmenu.MainActivity">

    <item
        android:id="@+id/action_item1"
        android:orderInCategory="100"
        android:title="@string/action_item1"
        app:showAsAction="never">

        <menu>
            <item
                android:id="@+id/action_sub_item1"
                android:orderInCategory="100"
                android:title="@string/action_sub_item1"
                app:showAsAction="never" />

            <item
                android:id="@+id/action_sub_item2"
                android:orderInCategory="100"
                android:title="@string/action_sub_item2"
                app:showAsAction="never" />
        </menu>

    </item>


    <item
        android:id="@+id/action_item2"
        android:orderInCategory="100"
        android:title="@string/action_item2"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_item3"
        android:orderInCategory="100"
        android:title="@string/action_item3"
        app:showAsAction="never" />


    <item
        android:id="@+id/action_item4"
        android:orderInCategory="100"
        android:title="@string/action_item4"
        app:showAsAction="never" />


</menu>



2. XML Layout


 file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Popup Menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


3. MainActivity 


Setting Popup Menu Inside Activity .
  • Inflating PopupMenu  :   create popmenu object and pass view on click of which popmenu appears on click of it
Handling PopupMenu item click
  1. On popupMenu reference call setOnMenuItemClickListener 
  2.  using MenuItem paramter identify the item's of popmenu click and handle
  3.  override onContextItemSelected() of Activity inside this callback method using MenuItem paramter identify the item's of context menu click and handle



file : MainActivity.java 
package com.tutorialsbuzz.androidpopupmenu

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.PopupMenu
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        btn.setOnClickListener({ view ->
            showPopupMenu()
        })

    }


    private fun showPopupMenu() {
        val popupMenu = PopupMenu(this, btn)
        popupMenu.inflate(R.menu.popup_menu)
        popupMenu.show();

        popupMenu.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item: MenuItem ->

            when (item.itemId) {

                R.id.action_item2 -> {
                    Toast.makeText(this@MainActivity, "action item2 clicked", Toast.LENGTH_SHORT).show()
                }
                R.id.action_item3 -> {
                    Toast.makeText(this@MainActivity, "action item3 clicked", Toast.LENGTH_SHORT).show()
                }

                R.id.action_item4 -> {
                    Toast.makeText(this@MainActivity, "action item4 clicked", Toast.LENGTH_SHORT).show()
                }

                R.id.action_sub_item1 -> {
                    Toast.makeText(this@MainActivity, "action subitem1 clicked", Toast.LENGTH_SHORT).show()
                }

                R.id.action_sub_item2 -> {
                    Toast.makeText(this@MainActivity, "action subitem2 clicked", Toast.LENGTH_SHORT).show()
                }

            }

            true
        })

    }


}





Menu Related
  1. Option Menu
  2. Context Menu
  3. Popup Menu

No comments:

Post a comment