Android Menu Brief analysis (ContextMenu)

Source: Internet
Author: User

In the previous article on the Android menu Optionsmenu to introduce the use of Optionsmenu, this article then to introduce you to ContextMenu usage.

ContextMenu Short-context menu, with long-press event response, there are two modes of response

    1. Floating mode
      Effect similar to pop-up Dialog, in the center of the screen, you can customize the display of the Menu and MenuItem response
    2. Action mode
      Through the Actionbar realization, the effect is really the Title of the place, the action Bar appears.

Special attention

ContextMenu Action mode is not available in versions prior to Android 3.0 (Action mode requires Actionbar support) if the app Sherlockactionbar support is temporarily not verified.

Let's take a look at the action pattern .


Csdn can't upload pictures.


The first picture: Long press after the display effect, the title of the App becomes Actionmode, will see our MenuItem only icon, no text.

Second Picture: Click More After, no display of MenuItem, only text, no icon.

And look at the floating mode of ContextMenu.


Csdn can't upload pictures.


Long press BTN, the popup screen, click on the MenuItem or outside the menu area, the menu disappears.

On the low version, before Android 3.0, the ContextMenu has the same floating effect, as shown, using the same method.

Now look at how to create the ContextMenu, as well as the method called.

First, the action mode

ContextMenu the action mode creation process

    1. Registering the action mode
To BTN registered action mode Registerforcontextmenu (Btnview); Response Long Press event Btn.setonlongclicklistener (new Onlongclicklistener () {     @Override public     boolean Onlongclick (View v) {         v.setselected (true);         ContextMenu.this.startActionMode (Mactionmode);         return true;     } });

2. Implement the action mode callback interface


 /** * * Create CALLBACK interface * */private Actionmode.callback Mactionmode = new Actionmode.callback () {@Override public boolean Onprepa Reactionmode (actionmode Mode, menu menu) {//TODO auto-generated method stub return false;} @Override public Voi D Ondestroyactionmode (Actionmode mode) {//TODO auto-generated method Stub if (mode = null) {mode = nul     L }} @Override public boolean oncreateactionmode (actionmode mode, menu menu) {menuinflater inflater = Getmenuinflater (     );     Add menu file Inflater.inflate (R.menu.main_icon, menu); return true; } @Override public boolean onactionitemclicked (actionmode mode, MenuItem Item) {switch (Item.getitemid ()) {case         R.ID.ACTION_SHARE:LOG.D (Tag, "Action_share");         Toast.maketext (Getapplicationcontext (), "Action_share", Toast.length_short). Show ();         Mode.finish ();     Break         Case R.ID.ACTION_SAVE:LOG.D (tag, "Action_save"); Toast.maketext (Getapplicationcontext (), "Action_Save ", Toast.length_short). Show ();         Mode.finish ();     Break         Case R.ID.ACTION_SETTINGS:LOG.D (tag, "action_settings");         Toast.maketext (Getapplicationcontext (), "action_settings", Toast.length_short). Show ();         Mode.finish ();     Break         Case R.ID.ACTION_DELETE:LOG.D (tag, "Action_delete");         Toast.maketext (Getapplicationcontext (), "Action_delete", Toast.length_short). Show ();         Mode.finish ();     Break         Case R.ID.ACTION_EDIT:LOG.D (tag, "Action_edit");         Toast.maketext (Getapplicationcontext (), "Action_edit", Toast.length_short). Show ();         Mode.finish ();     Break     Default:break; } return true; } };


3. Unregister action mode

Unregister Unregisterforcontextmenu (Btnview);

The menu XML file


<menu xmlns:android= "http://schemas.android.com/apk/res/android" ><item android:id= "@+id/action_share" android:orderincategory= "0" android:showasaction= "ifroom" android:icon= "@android:d rawable/ic_menu_share" Android: title= "Share"/><item android:id= "@+id/action_save" android:orderincategory= "1" android:showasaction= "IfRoom     "Android:icon=" @android:d rawable/ic_menu_save "android:title=" save "/><item android:id=" @+id/action_search " android:orderincategory= "2" android:showasaction= "Ifroom" android:icon= "@android:d rawable/ic_menu_search" and roid:title= "Search"/><item android:id= "@+id/action_delete" android:orderincategory= "4" android:showAsAction = "Ifroom" android:icon= "@android:d rawable/ic_menu_delete" android:title= "delectaction"/> <item android:id= " @+id/action_edit "android:orderincategory=" 5 "android:showasaction=" Ifroom "android:icon=" @android:d Rawable/ic_me Nu_edit "Android:title=" EDit "/> <item android:id=" @+id/action_help "android:orderincategory=" 6 "android:showasaction=" IfRoom "a ndroid:icon= "@android:d rawable/ic_menu_help" android:title= "Help"/> <item android:id= "@+id/action_compass "android:orderincategory=" 6 "android:showasaction=" Ifroom "android:icon=" @android:d Rawable/ic_menu_compass "a Ndroid:title= "Compass"/></menu>


Also, the action mode can be combined with the ListView and GridView

Long press the ListView Item to look like the effect


Dad's CSDN can't upload a picture.


Specific code implementation


    Listview.setchoicemode (Listview.choice_mode_multiple_modal); Listview.setmultichoicemodelistener (New Multichoicemodelistener () {@Override public boolean onprepareaction        Mode (actionmode mode, menu menu) {//TODO auto-generated method stub return false;         } @Override public void Ondestroyactionmode (Actionmode mode) {//TODO auto-generated method stub } @Override public boolean oncreateactionmode (actionmode mode, menu menu) {menuinflater            Inflater = Getmenuinflater ();            Inflater.inflate (R.menu.main_icon, menu);        return true; } @Override public boolean onactionitemclicked (actionmode mode, MenuItem Item) {Switch (item.get                ItemId ()) {case R.ID.ACTION_SHARE:LOG.D (tag, "Action_share");                Modify title Mode.settitle ("Action_share"); Toast.maketext (Getapplicationcontext (),"Action_share", Toast.length_short). Show ();                Mode.finish ();            Break                Case R.ID.ACTION_SAVE:LOG.D (tag, "Action_save");                Toast.maketext (Getapplicationcontext (), "Action_save", Toast.length_short). Show ();                Mode.finish ();            Break            Default:break;        } return true;             } @Override public void onitemcheckedstatechanged (actionmode mode, int position, long ID, Boolean checked) { TODO auto-generated Method stub}});


Second, floating mode

    1. Create a Menu
    2. Set Menu Listener Events

The specific code process

@Overridepublic void Oncreatecontextmenu (Android.view.ContextMenu menu, view V, Contextmenuinfo menuinfo) {    Menuinflater inflater = Getmenuinflater ();    Inflater.inflate (R.menu.main_icon, menu);    Super.oncreatecontextmenu (menu, V, menuinfo);} @Overridepublic boolean oncontextitemselected (MenuItem item) {    //listener event only made a partial response to    switch (Item.getitemid ()) { Case    R.id.action_compass: Break        ;    Case R.id.action_delete: Break        ;    Default: Break        ;    }    return super.oncontextitemselected (item);}

Menu XML file is the same as action mode

Although the menu has an icon, but ContextMenu does not support the icon, this official website has a description.

This completes the basic ContextMenu floating mode menu.

Several other methods related to ContextMenu



/**** Open menu ***/@Overridepublic void Opencontextmenu (view view) {    //TODO auto-generated method stub    Super.opencontextmenu (view);} /**** close menu ***/@Overridepublic void Closecontextmenu () {    //TODO auto-generated method stub    Super.closecontextmenu ();} /**** Listener Event after closing menu ***/@Overridepublic void oncontextmenuclosed (Menu menu) {    super.oncontextmenuclosed (menu);}



These methods can be implemented selectively according to their own needs.

ContextMenu The relevant knowledge to introduce to you here, there are any questions to welcome you to exchange.

The next article to introduce Popmenu 's relevant knowledge





Android Menu Brief analysis (ContextMenu)

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.