Context menu in android

By | March 16, 2011

What is context menu?
Where it is used?
How it is used?

I will cover all these questions…

Context Menu

A context menu is conceptually similar to the menu displayed when the user performs a “right-click” on a PC. You should use a context menu to provide the user access to actions that pertain to a specific item in the user interface. On Android, a context menu is displayed when the user performs a “long press” (press and hold) on an item.

By two ways we can enable Context menu from listView

First Create an xml to hold the ListView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
 	<ListView android:id="@+id/listview"
		 android:scrollbars="vertical"
	 	 android:layout_width="fill_parent"
	 	 android:layout_height="fill_parent"
	 	 android:layout_gravity="fill_vertical"
	  	 android:layout_alignParentTop="true" >
	</ListView>
</RelativeLayout>

Now its time to create a simple listview in java.
Create a listView and set up a itemClicklistener

package com.coderzheaven.pack;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Listview extends Activity {
	 ListView list;
	    private List List_file;
	    public void onCreate(Bundle savedInstanceState)
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        List_file =new ArrayList();
	        list = (ListView)findViewById(R.id.listview);
	 
	        CreateListView();
	    }
	    private void CreateListView()
	    {
	         List_file.add("bird");
	         List_file.add("cat");
	         List_file.add("dog");
	         List_file.add("tiger");
	         List_file.add("goat");
	         //Create an adapter for the listView and add the ArrayList to the adapter.
	         list.setAdapter(new ArrayAdapter(Listview.this, android.R.layout.simple_list_item_1,List_file));
	        // registerForContextMenu(list);
	 
	         list.setOnItemClickListener(new OnItemClickListener()
	         {
	                public void onItemClick(AdapterView arg0, View arg1,
	                        int arg2, long arg3)
	                {
	                }
	        });
	        
	    
	    list.setOnCreateContextMenuListener(new OnCreateContextMenuListener()
	    {
	       public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
	       {
	           // TODO Auto-generated method stub
	            menu.setHeaderTitle("ContextMenu");
	            for (int i = 0; i < 5; i++)
	            {
	                menu.add(0, 0,0, "favorite"+i);
	            }
	       }
	   });
	}
}

The ContextMenu will appear when we long pres the listItem. We can enable this by two method

First Method

 list.setOnCreateContextMenuListener(new OnCreateContextMenuListener()
 {
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
	{
		// TODO Auto-generated method stub
		 menu.setHeaderTitle("ContextMenu");
		 for (int i = 0; i&lt;5; i++)
		 {
			 menu.add(0, 0,0, "favorite"+i);
		 }
	}
});

Second Method
Instead of setting up a listener we can register contextMenu from listView by

registerForContextMenu(list);

Then when the user selects an item from the context menu, the system calls onContextItemSelected(). Here is an example of how you can handle selected items

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
		menu.setHeaderTitle("ContextMenu");
	    for (int i = 0; i&lt;5; i++)
	    {
	    	 menu.add(0, 0,0, "favorite"+i);
	    }
}

One thought on “Context menu in android

  1. Pingback: How to display a context menu from a fragment? | Android Forever

Leave a Reply

Your email address will not be published. Required fields are marked *