Context Menu
It is also called the long-pressed menu. When a view registers a context menu, a floating menu with relevant functions is displayed when you execute a long-pressed (2 seconds) Action on the object.
Class Overview
The context Menu expanded from Menu provides the ability to modify the context Menu header (header.
Context menus do not support shortcuts and icons of menu items.
The public method public abstract void clearHeader () clears information about the context menu header. (Including image and Text Information)
Public abstract ContextMenu setHeaderIcon (Drawable icon)
Set the icon for the context menu Header
Public abstract ContextMenu setHeaderIcon (int iconRes)
Set the context menu header icon to the specified resource id
Public abstract ContextMenu setHeaderTitle (int titleRes)
Use the resource identifier to set text in the title bar of the context menu header.
Public abstract ContextMenu setHeaderTitle (CharSequence title)
Set the title of the context menu, which is displayed in the title bar
Public abstract ContextMenu setHeaderView (View view)
Set the View to the context menu header. The icon and title of the context menu header will be replaced (or the headerView you set earlier)
Code example when the CEO uses context menus, The registerForContextMenu (View) function is called in most cases and the onCreateContextMenu (ContextMenu, View, ContextMenu. ContextMenuInfo) function is rewritten. To create a context menu, you must override the context callback function onCreateContextMenu () of the activity and register the context menu for it through registerForContextMenu (View.
Registration context menu
RegisterForContextMenu (lv); // Add context menu for ListView
Add menu item
@ Overridepublic void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {// TODO Auto-generated method stub try {cmdinfo = (AdapterContextMenuInfo) menuInfo; JSONObject object = mData. getJSONObject (info. position); menu. setHeaderTitle (object. getString ("title");} catch (Exception e) {return;} menu. add (0, 1, 0, "delete chat"); menu. add (0, 2, 0, "Top chat"); super. onCreateContextMenu (menu, v, menuInfo) ;}@ Overridepublic boolean onContextItemSelected (MenuItem item) {// TODO Auto-generated method stubAdapterContextMenuInfo itemInfo = (AdapterContextMenuInfo) item. getMenuInfo (); try {JSONObject object = mData. getJSONObject (itemInfo. position); switch (item. getItemId () {case 1: Toast. makeText (this, object. getString ("title"), Toast. LENGTH_LONG ). show (); break; default: break;} catch (JSONException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return super. onContextItemSelected (item );}
Long press "name 2"
Click "delete chat"