Creating Contextual Menus create context menu

Source: Internet
Author: User

A contextual menu offers actions that affect a specific item or context frame in the UI. you can provide a context menu for any view, but they are most often used for items in a ListView, GridView, or other view collections in which the user can perform direct actions on each item.

A context menu provides actions that affect a specific project or context framework in the UI. You can provide a context menu for any point of view, but they are usually used for projects in the List View, displaying data tables, or other sets of views, you can perform operations directly on each project.

There are two ways to provide contextual actions:

There are two ways to provide related operations:


In a floating context menu. A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long-click (press and hold) on a view that declares support for a context menu. users can perform a contextual action on one item at a time.
In the contextual action mode. this mode is a system implementation ofActionMode that displays a contextual action bar at the top of the screen with action items that affect the selected item (s ). when this mode is active, users can perform an action on multiple items at once (if your app allows it ).

Note: The contextual action mode is available on Android 3.0 (API level 11) and higher and is the preferred technique for displaying contextual actions when available. if your app supports versions lower than 3.0 then you shoshould fall back to a floating context menu on those devices.

In a floating context menu. A menu is displayed as a floating menu item list (similar to a dialog box). You can execute a long click (pressed and kept) in a view to announce that a context menu is supported. You can execute context actions in a project at a time. In the context action mode. This mode is a system-implemented ActionMode. The context operation bar is displayed at the top of the screen and the action project affects the selected project (s ). When this mode is activated, You can execute an action in multiple projects at one time (if your application allows it ). Note: The context action mode is available when Android 3.0 (API level 11) and higher and is the preferred technology to display related operations. If your application supports version earlier than 3.0, you should roll back to a floating context menu on those devices.



Creating a floating context menu to create a floating context menu

To provide a floating context menu:

Register the View to which the context menu shoshould be associated by calling registerForContextMenu () and pass it the View.
If your activity uses a ListView or GridView and you want each item to provide the same context menu, register all items for a context menu by passing the ListView or GridView to registerForContextMenu ().

Implement the onCreateContextMenu () method in your Activity or Fragment.
When the registered view has es a long-click event, the system callyour onCreateContextMenu () method. This is where you define the menu items, usually by inflating a menu resource. For example:

@ Override
Public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo ){
Super. onCreateContextMenu (menu, v, menuInfo );
MenuInflater inflater = getMenuInflater ();
Inflater. inflate (R. menu. context_menu, menu );
} MenuInflater allows you to inflate the context menu from a menu resource. the callback method parameters include the View that the user selected and a ContextMenu. contextMenuInfo object that provides additional information about the item selected. if your activity has several views that each provide a different context menu, you might use these parameters to determine which context menu to inflate.

Implement onContextItemSelected ().
When the user selects a menu item, the system callthis method so you can perform the appropriate action. For example:

@ Override
Public boolean onContextItemSelected (MenuItem item ){
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item. getMenuInfo ();
Switch (item. getItemId ()){
Case R. id. edit:
EditNote (info. id );
Return true;
Case R. id. delete:
DeleteNote (info. id );
Return true;
Return super. onContextItemSelected (item );
} The getItemId () method queries the ID for the selected menu item, which you shoshould assign to each menu item in XML using the android: id attribute, as shown in the section about Defining a Menu in XML.

When you successfully handle a menu item, return true. if you don't handle the menu item, you should pass the menu item to the superclass (superclass) implementation. if your activity has des fragments, the activity has es this callback first. by calling the superclass when unhandled, the system passes the event to the respective callback method in each fragment, one at a time (in the order each fragment was added) until trueor false is returned. (The default implementation for Activity and android. app. fragment return false, so you shoshould always call the superclass (superclass) when unhandled .)



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: 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.