Using the context menu for Android development

Source: Internet
Author: User

The context menu in Android is similar to the right mouse click on a PC, unlike the concept that there is no mouse on Android, much less right-click on Android, which is typically long press a view to bring up the context menu. Unlike Optionsmenu, ContextMenu is a menu for view that is bound to a view and Optionsmenu belongs to an activity.


Most browsers now have long-press copy functionality, such as:


The implementation of this effect is implemented using Popupwindow (for the use of Popupwindow can be found in one of my other articles Popupwindow use the detailed
), though it's much like the ContextMenu we're about to say.


OK, so much to say, we should talk about how to implement the context menu.

There are three steps to implementing a context menu:

1. Rewrite the Oncreatecontextmenu method in activity or fragment:

@Overridepublic void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) { Super.oncreatecontextmenu (menu, V, menuinfo); Menu.setheadertitle ("What do you want to do?" Menu.setheadericon (R.DRAWABLE.A4C); menu.add (0, 0, menu.none, "copy"), Menu.add (0, 1, menu.none, "clip"); Menu.add (0, 2, Menu.none, "renaming"); Menu.add (1, 3, Menu.none, "go to New Activity");}


2. Override the Oncontextitemselected method in activity or fragment to implement menu event monitoring

@Overridepublic boolean oncontextitemselected (MenuItem item) {switch (Item.getitemid ()) {Case 0:tv.settext ( Item.gettitle (). ToString ()), Break;case 1:tv.settext (Item.gettitle (). ToString ()), Break;case 2:tv.settext ( Item.gettitle (). ToString ()), Break;case 3:tv.settext (Item.gettitle (). ToString ()); StartActivity (New Intent (This, Secondactivity.class)); Break;default:return super.oncontextitemselected (item);} return true;}

3. Register the context menu for the view

TV = (TextView) This.findviewbyid (r.id.tv); This.registerforcontextmenu (TV);

three steps implemented to register a context menu event for a textview, sometimes we encounter another situation, for example to add a context menu to each item in the ListView, and when we press and hold the ListView item, We also want to be able to know which item we clicked on, how can this be achieved? Adaptercontextmenuinfo is designed to solve this problem, look at the following code:

public class Secondactivity extends Activity {private ListView lv;private string[] menustrs;private arrayadapter< string> adapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_second); LV = (ListView) This.findviewbyid (r.id.lv); Initlistview (); This.registerforcontextmenu (LV);} private void Initlistview () {menustrs = new string[] {"Celebration four Spring", "Teng Exile Shou-Ling County", "more next year", "harmonious"};adapter = new arrayadapter< String> (this,android. R.layout.simple_list_item_1, Menustrs); Lv.setadapter (adapter);} @Overridepublic void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) { Super.oncreatecontextmenu (menu, V, menuinfo); Menu.setheadertitle ("What do you want to do?" "); Menu.setheadericon (R.DRAWABLE.A4C); menu.add (0, 0, Menu.none," Yue "), Menu.add (0, 1, menu.none," Yang "); Menu.add (0, 2, Menu.none, "Lou"); Menu.add (1, 3, Menu.none, "Kee");} @Overridepublic boolean oncontextitemselected (MenuItem item) {Adaptercontextmenuinfo Menuinfo = (AdapTercontextmenuinfo) item.getmenuinfo (); switch (Item.getitemid ()) {case 0:menustrs[menuinfo.position] + = "Yue"; Case 1:menustrs[menuinfo.position] + = "yang"; break;case 2:menustrs[menuinfo.position] + = "Lou"; Break;case 3:menuStrs[ Menuinfo.position] + = "kee"; Break;default:return super.oncontextitemselected (item);} Adapter.notifydatasetchanged (); return true;}}

The front of the things are very simple, I will not say, through the example of adaptercontextmenuinfo we can get the position,id we clicked on the item, and one of the effects we achieved here is to click on the context menu after each item Add a word after view.


The effect of the implementation is as follows:



Demo Download Https://github.com/lenve/contextMenu



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Using the context menu for Android development

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.