[Android] use of context menu ContextMenu

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.