Android - Kotlin - ContextMenu - How to Create Contextmenu in Android?

In this post we are going to learn how to create ContextMenu in Android

To Make any view to show context menu we need to register that View with below code

registerForContextMenu(View)

and we can unregister with 

unregisterForContextMenu(View)

 

Let's Start

Step 1: Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project

Step 2: Updated Activity xml file with below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        tools:context=".contextmenu.ContextMenuActivity">

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/btnContext" android:text="Long Press"
            android:layout_gravity="center"
            android:background="@color/colorPrimary"
            android:textColor="@color/white"
            android:minEms="9"
            android:textSize="20dp"

    />
</LinearLayout>

 

Step 3: Create Menu file and add below code

<?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">
    <item android:id="@+id/coupons"
          android:title="Coupons"
          app:showAsAction="never"></item>
    <item android:id="@+id/cashback"
          android:title="Cashback"
          app:showAsAction="never"></item>
    <item android:id="@+id/deals"
          android:title="Deals"
          app:showAsAction="never"></item>
</menu>
 

 

Step 4: Update Activity with below code

package com.rrtutors.androidsamples.contextmenu

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.rrtutors.androidsamples.R
import kotlinx.android.synthetic.main.activity_context_menu.*
import android.widget.Toast
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View


class ContextMenuActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.rrtutors.androidsamples.R.layout.activity_context_menu)
        registerForContextMenu(btnContext)
        btnContext

    }

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

        val inflater = menuInflater
        inflater.inflate(R.menu.menu, menu)
    }



    override fun onContextItemSelected(item: MenuItem): Boolean {
        var toast: Toast? = null
        when (item.getItemId()) {
           R.id.coupons -> {
                toast = Toast.makeText(this, "Coupons context item clicked", Toast.LENGTH_LONG)
                toast!!.show()
                return true
            }
           R.id.cashback -> {
                toast = Toast.makeText(this, "Cashback context item clicked", Toast.LENGTH_LONG)
                toast!!.show()
                return true
            }
         R.id.deals -> {
                toast = Toast.makeText(this, "Deals context item clicked", Toast.LENGTH_LONG)
                toast!!.show()
                return true
            }
            else -> return super.onContextItemSelected(item)
        }
    }
}

 

Step 5: Let's run application

Kotlin - Android ContextMenu

 

Kotlin - Android ContextMenu