This code example uses an Activity that extends ListActivity and creates a context menu that appears if you long-touch any row in the ListView.
Main Steps
1. Create an XML context menu file in res/menu.
Example:
context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/image" android:title="@string/image"></item>
<item android:id="@+id/video" android:title="@string/video"></item>
<item android:id="@+id/edit" android:title="@string/edit"></item>
</menu>
If you use the Eclipse plug-in graphical form to create the context menu, you might want to change the default ids to something more descrptive than id/item1, id/item2, etc.
2. Create an activity that extends ListView.
The ListView in an activity that extends ListActivity must use this ID: android:id="@android:id/list"
See: http://developer.android.com/reference/android/app/ListActivity.html
3. Register the ListView using a statement such as this:
MyListView = getListView();
Note: This is instead of using findViewById()
4. Register the ListView with your context menu.
Call registerForContextMenu() and pass to it the View from which you want to launch your context menu.
5. Inflate your context menu in the default activity's onCreateContextMenu() method. Inflating the menu means transforming the menu XML file to a menu object.
http://developer.android.com/guide/topics/ui/menus.html#context-menu
"When the registered view receives a long-click event, the system calls your onCreateContextMenu() method. This is where you define the menu items, usually by inflating a menu resource."
6. Implement onContextItemSelected()
The system calls this method when a user selects an item in the ListView.
You can determine the 1) context menu item selected using the getItemId() method, and 2) the position of the underlying ListView row that was long-clicked by using menuInfo.position.
Example code
import android.app.ListActivity;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainContextMenuActivity extends ListActivity {
// This array holds the data for displaying ListView items
String[] fruit = { "Apricot", "Blackberry", "Dragonfruit", "Grapefruit",
"Kumquat", "Orange", "Persimmon", "Quince", "Strawberry",
"Watermelon" };
ListView lv_fruit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Register the ListView
lv_fruit = getListView();
// Connect a new ArrayAdapter, default layout, and the data source.
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fruit));
registerForContextMenu(lv_fruit);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast toast = Toast.makeText(this, fruit[position], Toast.LENGTH_LONG);
toast.show();}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AdapterView.AdapterContextMenuInfo menuInfo;
// AdapterView.AdapterContextMenuInfo provides information about this:
// id The row id of the item for which the context menu is being displayed.
// position The position in the adapter for which the context menu is being displayed.
menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = menuInfo.position;
switch (item.getItemId()) {
case R.id.image:
Toast.makeText(
this,
"Clicked Image context menu at ListView position: "
+ position, Toast.LENGTH_LONG).show();
break;
case R.id.video:
Toast.makeText(this,
"Clicked Video context menu at position: " + position,
Toast.LENGTH_LONG).show();
break;
case R.id.edit:
Toast.makeText(this,
"Clicked Edit context menu at position: " + position,
Toast.LENGTH_LONG).show();
return true;
}
return false;
}
}