Android Context Menu In Kotlin

A context menu is a floating menu that appears when the user performs a long-click on an element. 
It provides actions that affect the selected content or context frame.

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

1. Menu XML Layout 


Create Menu In Android Studio 

Android Context Menu



Android Context Menu


file : res/menu/content_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:icon="@mipmap/ic_launcher_round"
        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


Add button to and we will register for content for this button .

file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</LinearLayout>


3. MainActivity 


Setting Content Menu Inside Activity .

  1. Override onCreateContextMenu() of Activity inside this callback method inflate the content_menu from res/menu/menu_main.xml .
  2. Handling Context menu item click Override onContextItemSelected() of Activity inside this callback method using MenuItem paramter identify the item's of context menu click and handle

file : MainActivity.kt
package com.tutorialsbuzz.androidcontextmenu

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
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)

        registerForContextMenu(btn)

    }

    override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)

        menu?.setHeaderTitle("Context Menu Header")
        menuInflater.inflate(R.menu.context_menu, menu)

    }

    override fun onContextItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {

            R.id.action_item2 -> {
                // handing on click of item with id action_item2
                Toast.makeText(this@MainActivity, "item2", Toast.LENGTH_SHORT).show()
                true
            }

            R.id.action_item3 -> {
                // handing on click of item with id action_item3
                Toast.makeText(this@MainActivity, "item3", Toast.LENGTH_SHORT).show()
                true
            }

            R.id.action_item4 -> {
                // handing on click of item with id action_item4
                Toast.makeText(this@MainActivity, "item4", Toast.LENGTH_SHORT).show()
                true
            }

            R.id.action_sub_item1 -> {
                // handing on click of item with id action_item4
                Toast.makeText(this@MainActivity, "sub item1", Toast.LENGTH_SHORT).show()
                true
            }

            R.id.action_sub_item2 -> {
                // handing on click of item with id action_item4
                Toast.makeText(this@MainActivity, "sub item2", Toast.LENGTH_SHORT).show()
                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }

}


Android Context Menu

Android Context Menu


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

No comments:

Post a Comment