Pro Android Learning Note (32): Menu (3): Context Menu

Source: Internet
Author: User

What is the context menu

On the desktop, we are familiar with the context menu, the menu that is displayed by the right mouse button is the context menus. In Android, a context menu can be triggered by a long mouse button. For touch-screen devices, long-press the finger is the long mouse button event.

An activity has one and only one optionmenu, a view can have one and can have at most one contextmenu. Check the mouse long press is based on the view to listen. So how many view can there be in an activity, and how many ContextMenu.

Register View with context menu

If a view has a context menu, you need to first register it in OnCreate (), which is the code below

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.pure_text);
In a small example, there is only one full-screen TextView
Infotv = (TextView) Findviewbyid (r.id.pureinfo);
//"Step 1": Register the context menu for view in OnCreate ()
Registerforcontextmenu
(INFOTV);
}

Fill in the context menu contents

After registering, we want to write the menu item specifically, will rewrite Oncreatecontextmenu (), the example code is as follows:

private static int tv_contextmenu_base = 1000;
The first parameter is a pre-ContextMenu object, the second parameter is the registered view, the third parameter is generally not used, but sometimes the view needs to pass the information through this parameter. Contextmenuinfo are interfaces that implement classes that have Adaptercontextmenuinfo and Expandablecontextmenu. If the information passed is related to the cursor, you can use Adaptercontextmenuinfo to pass the ROWID.
Public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo){
Super.oncreatecontextmenu (menu, V, menuinfo);
Set a title icon and title and add two menu items
Menu.Setheadericon(R.drawable.leaf);
Menu.Setheadertitle("Context menu:");
Menu.Add(Menu.none,Tv_contextmenu_base,Tv_contextmenu_base, "item.1");
Menu.Add(Menu.none,Tv_contextmenu_base + 1,Tv_contextmenu_base + 1, "item.2");

Sub-menu Nesting test
Submenu submenu = Menu.addsubmenu ("Sub-menu");
Submenu.setheadericon (R.drawable.clock);
Submenu.add (Menu.none,tv_contextmenu_base + +, + tv_contextmenu_base + +, "sub.1");
Submenu.add (Menu.none,tv_contextmenu_base + 101, + Tv_contextmenu_base + 101, "Sub.2");
}

ContextMenu and Optionmenu are different in initialization, optionmenu occurs when activity is created, and ContextMenu is triggered each time a mouse event is pressed. For the same view, Oncreatecontextmenu () is called each time a long key is pressed. It is important to note that each time a new ContextMenu object is returned, that is, ContextMenu is created in real time.

Context Menu click Trigger

ContextMenu is the implementation of the menu interface, and the triggering mechanism is very similar. The example code is as follows:

@Override
Public Boolean oncontextitemselected (MenuItem item) {
if (item.getitemid () = = tv_contextmenu_base) {
Showinfo ("Item.1 was clicked");
}else{
Showinfo ("ItemId" + item.getitemid () + "have nothing Done");
}
return super.oncontextitemselected (item);
}

This post covers example code that can be downloaded in Pro Android Learning: Menu.

RELATED Links: My Android development related articles

Pro Android Learning Note (32): Menu (3): Context Menu

Related Article

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.