The context menu is different from the options menu,The option menu serves the activity, and the context menu is registered to a view object.

If a view object is registered with the context menu,The user can call the context menu by long pressing the view object.

The context menu does not support shortcut keys.The menu options cannot be accompanied by icons.However, you can specify an icon for the title of the context menu.

Following mock context menu

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:id="@ + id/linearlayout01"
  <edittext android:text="first text box"
    android:id="@ + id/edittext01"
    android:layout_height="wrap_content" />
  <edittext android:text="second text box"
    android:id="@ + id/edittext02"
    android:layout_height="wrap_content" />

meunactivity class

package com.ljq.activity;
import android.app.activity;
import android.os.bundle;
import android.view.contextmenu;
import android.view.menuitem;
import android.view.view;
import android.view.contextmenu.contextmenuinfo;
import android.widget.edittext;
public class meunactivity extends activity {
  private edittext edittext01=null;
  private edittext edittext02=null;
  private final int menu1=1;
  private final int menu2=2;
  private final int menu3=3;
  private final int menu4=4;
  private final int menu5=5;
  protected void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.main);
    edittext01=(edittext) findviewbyid (r.id.edittext01);
    edittext02=(edittext) findviewbyid (r.id.edittext02);
    //Register the context menu for the view object
    this.registerforcontextmenu (edittext01);
    this.registerforcontextmenu (edittext02);
   * Initialize the context menu
   * Called each time the context menu is called up
   * /
  public void oncreatecontextmenu (contextmenu menu, view v,      contextmenuinfo menuinfo) {
    menu.setheadericon (r.drawable.header);
    switch (v.getid ()) {
    case r.id.edittext01:
      menu.add (0, menu1, 0, "menu item 1");
      menu.add (0, menu2, 0, "menu item 2");
      menu.add (0, menu3, 0, "menu item 3");
    case r.id.edittext02:
      menu.add (0, menu4, 0, "menu item 4");
      menu.add (0, menu5, 0, "menu item 5");
    //super.oncreatecontextmenu(menu, v, menuinfo);
   * This event is called when the user selects the context menu option
   * /
  public boolean oncontextitemselected (menuitem item) {
    switch (item.getitemid ()) {
    case menu1:
    case menu2:
    case menu3:
      edittext01.append ("\ n" + item.gettitle () + "pressed");
    case menu4:
    case menu5:
      edittext02.append ("\ n" + item.gettitle () + "pressed");
    return true;

operation result:

  • Previous Email regular expressions collation
  • Next Batch processing perfectly realizes FTP remote backup data