Browse By

Context Menu in Android

Almost every application will need a menu in order to facilitate a user to perform actions on the application. In Android, there are three types of menus possible.

  1. Options Menu
  2. Context Menu
  3. Popup Menu

Context Menu is a floating list of menu items that appears when a user touches and holds a particular item displayed in the view, which has a menu associated with it. For associating menu with view,  registerForContextMenu() is used.

There are two ways of creating a Context Menu in your application. One is by instantiating the Menu class and the other is by inflating a Menu from an XML menu resource.  Based on best practices it is always better to define the Menu in an XML and inflate it in your code.

In this tutorial, we will create a simple android application which will display Context Menu by inflating Menu from XML menu resource. It will display four menu items as follows; View Record, Edit Record, Delete Record and Save Record  when someone click on Product List View Item.

Direct Download Full Source Code



Context Menu in Android

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2. Now Create context_menu.xml file in Menu Resource. It will be created in res/menu folder. Put below code in that file
    <?xml version="1.0" encoding="utf-8"?>
        <item android:title="View Record" android:id="@+id/menuView"></item>
        <item android:title="Edit Record" android:id="@+id/menuEdit"></item>
        <item android:title="Delete Record" android:id="@+id/menuDelete"></item>
        <item android:title="Save Record" android:id="@+id/menuSave"></item>
  3. Now create products list in string.xml (res/menu) file. Put following code for creating products list
    <?xml version="1.0" encoding="utf-8"?>
        <string name="app_name">Pulse7 Context Menu</string>
        <string-array name="products">
            <item>Digital Clock</item>
  4. Now open main.xml (res/layout) and put listview widget .
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:orientation="vertical" android:layout_width="fill_parent"
    	<ListView android:layout_height="wrap_content"
    		android:layout_width="match_parent" android:id="@+id/lvProducts"></ListView>
  5. Now open your main Activity class file and override methods ;  onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) for inflating context menu when someone long press on product listview, and onContextItemSelected(MenuItem item) to perform action when some one click on context menu item.
    package com.vrs.contextmenu;
    import android.os.Bundle;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.AdapterView.AdapterContextMenuInfo;
    import android.widget.Toast;
    public class Pulse7ContextMenuActivity extends Activity {
    	// Widget GUI
    	ListView lvProducts;
    	// Array Adapter
    	ArrayAdapter<String> adapter;
    	// String array
    	String products[];
    	/** Called when the activity is first created. */
    	public void onCreate(Bundle savedInstanceState) {
    		// Init Widget GUI
    		lvProducts = (ListView) findViewById(;
    		// Get String Array Resource from strings.xml
    		products = getResources().getStringArray(R.array.products);
    		// Iniit Adapter and attached with listview
    		adapter = new ArrayAdapter<String>(this,
    				android.R.layout.simple_list_item_1, products);
    		// Register List View for listing long press event to display context menu
    	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    		super.onCreateContextMenu(menu, v, menuInfo);
    		// Inflate Menu from xml resource
    		MenuInflater menuInflater = getMenuInflater();
    		menuInflater.inflate(, menu);
    	public boolean onContextItemSelected(MenuItem item) {
    		// Perform action accordingly based on context menu item clicked
    		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
    		final String prdouct = products[(int)];
    		switch (item.getItemId()) {
    					prdouct + " View event fired ", Toast.LENGTH_LONG).show();
    			return true;
    					prdouct + " Edit event fired ", Toast.LENGTH_LONG).show();
    			return true;
    					prdouct + " Delete event fired ", Toast.LENGTH_LONG).show();
    			return true;
    					prdouct + " Save event fired ", Toast.LENGTH_LONG).show();
    			return true;
    			return super.onContextItemSelected(item);
  6. Now execute application
    Context Menu in Android

    Long press on any product, it will display context menu

    Android Context Menu Example

    Context Menu is visible, Click on any menu item

    Android Context Menu Example

    Action is perform based on menu selected

Points to Remember

  • registerForContextMenu() is use for attaching context menu with any view.
  • onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) method need to override for inflating context menu from XML resource and display on screen.
  • getMenuInflater() method use for getting MenuInflater class object.
  • inflate()  method of MenuInflater class, inflate your xml resource .
  • onContextItemSelected(MenuItem item) method need to override for performing action on each item of menu.

I hope you like this article. Share your views to to improve content.

Download Full Source Code