Context Menu In Android With Example

Hello friends, today I will explain you how to build a context Menu in Android with example. The context menu is also called Long Press Menu.

So the context menu is the one which is in the form of a list and it appears when the user hold a particular item within list or it can be applied on the button.

For Example: In Whatsapp, when you hold any particular contact within the list, a menu is opened with various options to select, that menu is called Context Menu.

Now we know what actually is context menu, we will now do the implementation of this.

Context Menu in Android

  1. Create a project in Android and name it as Context Menu.
  2. After creating the project, now create an xml file and name it as ids.xml and do following changes in it:
<?xml version="1.0" encoding="utf-8"?>
    <item name="delete" type="id">delete</item>
    <item name="update" type="id">update</item>
    <item name="select" type="id">select</item>

3. Now go to the layout file content_main.xml and do following changes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:text="Context Menu"
        android:layout_centerHorizontal="true" />

Now go to file and do following changes:

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class ContextMenu extends ActionBarActivity implements View.OnClickListener{

    Button button;

    public View onCreate(Bundle savedInstanceState) {
        button = (Button)findViewById(;


    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE,, Menu.NONE, "Delete");
        menu.add(Menu.NONE,, Menu.NONE, "Update");
        menu.add(Menu.NONE,, Menu.NONE, "Select");


    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
                Toast.makeText(getActivity(),"Delete menu hit",Toast.LENGTH_SHORT).show();
                Toast.makeText(getActivity(),"Update menu hit",Toast.LENGTH_SHORT).show();
                Toast.makeText(getActivity(),"Select menu hit",Toast.LENGTH_SHORT).show();
        return super.onContextItemSelected(item);

Now I will explain the above code one by one.

  • Firstly we have declared a button and its id is find in the onCreate method.
  • Now to create a context menu, we have to override two of its methods, these are: onCreateContextMenu  and onContextItemSelected.
  • In onCreateContextMenu, we add the list of items we want to add within the context menu, here we have taken three values for the list.
  • Next comes the onContextItemSelected, in this we will define what action will be preformed after the item within the context menu is selected.
  • Here I have just used a toast method, you can use intent to preform different activities.

So as we have created the context menu, now is the time to run it.

Output 1:



So we have completed our application. Hope you enjoyed the tutorial.