Context Menu: How to create context menu with Example in Android using Kotlin.

Last updated Dec 17, 2021

In this article, we will see how to create a context menu and set onCLick on context menu items in Android Studio by using Kotlin Language.
 

In Android, the Context Menu is similar to  a floating menu that appears when the user long presses  or clicks on an item and is useful for creating methods that define the specific content or reference frame impact.
The Android context menu is alike to the right-click menu in Windows or Linux.

 

Implementation:

Create a new Project in android studio.

 Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish

 

Go to activity_context.xml file and add the following code

   <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:gravity="center"
    android:id="@+id/linearLayout"
    tools:context=".ContextActivity">

    <TextView
        android:id="@+id/pressMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Long Press Me"
        android:textSize="30sp"
        android:textStyle="italic"
        android:textColor="@color/black"></TextView>
</LinearLayout>

 

Create a new directory for menu items layout

app > res > right-click > new > Directory > enter name (menu) > Enter.

 

Create a new menu resource file 

app > res > menu > right-click > new > menu resource file (menu_items) > OK.

Add the following code to menu_items.xml file

   <?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="example.javatpoint.com.kotlincontextmenu.MainActivity">
    <item
        android:id="@+id/green"
        android:title="Green"
        app:showAsAction="never" />
    <item
        android:id="@+id/yellow"
        android:title="Yellow"
        app:showAsAction="never" />
    <item
        android:id="@+id/gray"
        android:title="Gray"
        app:showAsAction="never" />
</menu>

 

 

Open ContextActivity.kt file and add the following code.

lateinit var linearLayout: LinearLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_context)
        val pressMe = findViewById(R.id.pressMe)
        linearLayout  = findViewById(R.id.linearLayout)
        registerForContextMenu(pressMe)

    }
    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        super.onCreateContextMenu(menu, v, menuInfo)
        val inflater = menuInflater
        inflater.inflate(R.menu.menu_items, menu)
    }


    override fun onContextItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        when(id){
            R.id.green ->{
                linearLayout.setBackgroundColor(Color.GREEN)
                return true
            }
            R.id.yellow ->{
                linearLayout.setBackgroundColor(Color.YELLOW)
                return true
            }
            R.id.gray ->{
                linearLayout.setBackgroundColor(Color.GRAY)
                return true
            }
            else -> return super.onContextItemSelected(item)

        }
    }

 

For set Onclick on menu items use

override fun onContextItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        when(id){
            R.id.green ->{
                linearLayout.setBackgroundColor(Color.GREEN)
                return true
            }
            R.id.yellow ->{
                linearLayout.setBackgroundColor(Color.YELLOW)
                return true
            }
            R.id.gray ->{
                linearLayout.setBackgroundColor(Color.GRAY)
                return true
            }
            else -> return super.onContextItemSelected(item)

        }
    }

Run the app on emulator or real device, you will get the output as following in video

 

Complete Source Code of Context Menu Example 

activity_context.xml file

<?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:gravity="center"
    android:id="@+id/linearLayout"
    tools:context=".ContextActivity">

    <TextView
        android:id="@+id/pressMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Long Press Me"
        android:textSize="30sp"
        android:textStyle="italic"
        android:textColor="@color/black"></TextView>
</LinearLayout>

 ContextActivity.kt file

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView

class ContextActivity : AppCompatActivity() {

    lateinit var linearLayout: LinearLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_context)
        val pressMe = findViewById(R.id.pressMe)
        linearLayout  = findViewById(R.id.linearLayout)
        registerForContextMenu(pressMe)

    }
    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        super.onCreateContextMenu(menu, v, menuInfo)
        val inflater = menuInflater
        inflater.inflate(R.menu.menu_items, menu)
    }


    override fun onContextItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        when(id){
            R.id.green ->{
                linearLayout.setBackgroundColor(Color.GREEN)
                return true
            }
            R.id.yellow ->{
                linearLayout.setBackgroundColor(Color.YELLOW)
                return true
            }
            R.id.gray ->{
                linearLayout.setBackgroundColor(Color.GRAY)
                return true
            }
            else -> return super.onContextItemSelected(item)

        }
    }
}

menu_items.xml file

  <?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="example.javatpoint.com.kotlincontextmenu.MainActivity">
    <item
        android:id="@+id/green"
        android:title="Green"
        app:showAsAction="never" />
    <item
        android:id="@+id/yellow"
        android:title="Yellow"
        app:showAsAction="never" />
    <item
        android:id="@+id/gray"
        android:title="Gray"
        app:showAsAction="never" />
</menu>

 

Conclusion: In this article we have covered how to create and set OnClick event on context menu in Android Studio by using Kotlin Language.

<?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:gravity="center"
    android:id="@+id/linearLayout"
    tools:context=".ContextActivity">

    <TextView
        android:id="@+id/pressMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Long Press Me"
        android:textSize="30sp"
        android:textStyle="italic"
        android:textColor="@color/black"></TextView>
</LinearLayout>

 ContextActivity.kt file

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextMenu
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView

class ContextActivity : AppCompatActivity() {

    lateinit var linearLayout: LinearLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_context)
        val pressMe = findViewById(R.id.pressMe)
        linearLayout  = findViewById(R.id.linearLayout)
        registerForContextMenu(pressMe)

    }
    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        super.onCreateContextMenu(menu, v, menuInfo)
        val inflater = menuInflater
        inflater.inflate(R.menu.menu_items, menu)
    }


    override fun onContextItemSelected(item: MenuItem): Boolean {
        val id = item.itemId
        when(id){
            R.id.green ->{
                linearLayout.setBackgroundColor(Color.GREEN)
                return true
            }
            R.id.yellow ->{
                linearLayout.setBackgroundColor(Color.YELLOW)
                return true
            }
            R.id.gray ->{
                linearLayout.setBackgroundColor(Color.GRAY)
                return true
            }
            else -> return super.onContextItemSelected(item)

        }
    }
}

menu_items.xml file

  <?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="example.javatpoint.com.kotlincontextmenu.MainActivity">
    <item
        android:id="@+id/green"
        android:title="Green"
        app:showAsAction="never" />
    <item
        android:id="@+id/yellow"
        android:title="Yellow"
        app:showAsAction="never" />
    <item
        android:id="@+id/gray"
        android:title="Gray"
        app:showAsAction="never" />
</menu>

 

Conclusion: In this article we have covered how to create and set OnClick event on context menu in Android Studio by using Kotlin Language.

-->
Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1384 Views