Context Menu Android

The menu in Android can be of three types, mainly, Option menu, Context Menu, and Popup menu. On pressing a long click on an element, the Android context menu appears which is also known as the floating menu. While acting, the context menu affects the selected content. The item shortcuts and icons are not supported by the Android Context menu.

Android Context Menu Example:

In the below example, we are demonstrating the use of the context menu in android.


In the activity_main.xml file, we will drag a listview from the palette.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:text="Long press button"
        android:layout_marginTop="200dp" android:layout_marginLeft="100dp"/>

Activity class:(File:

In the file, we will write the code to display the context menu on the press of the listview.

package com.example.radioapp;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        Button btn=findViewById(;
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Context Menu");
        menu.add(0, v.getId(), 0, "A");
        menu.add(0, v.getId(), 0, "B");
        menu.add(0, v.getId(), 0, "C");
        menu.add(0, v.getId(), 0, "D");
    public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(this, "Selected Item: " +item.getTitle(), Toast.LENGTH_SHORT).show();
        return true;

Output 1:

Output 2:

Please follow and like us:
Content Protection by