Context Menu

   

When you hold your finger on a view long enough, a ContextMenu appears. The context menu you get depends on which view you press; In the above picture, I pressed the red TextView. The context menu appears at the center of the screen. See Creating a Context Menu, and the methods registerForContextMenu, onCreateContextMenu, onContextItemSelected of class Activity.

The Menu.NONE as the first argument of add means that the MenuItem is not part of a group. The second and third arguments of add are the item id (unique; used in onContextItemSelected) and the order. The fourth argument is a CharSequence, and class String implements the interface CharSequence.

I picked a textSize of 18 scaled pixels to agree with the text in the ActionBar.

Source code in Context.zip

  1. ContextActivity.java: I created the menus here, although I could also have created them in .xml files.
  2. R.java
  3. main.xml: three TextViews in a LinearLayout: red, green, blue.
  4. AndroidManifest.xml

Things to try

  1. The third argument of add is a 16-bit non-negative integer controlling the relative order in which the MenuItems appear in the menu. Use Menu.NONE if you don’t care. Use Short.MAX_VALUE - i to reverse the order.