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 inListView
,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
ActionMode
That
DisplaysContextual action barAt 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
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
View
To which
The context menu shoshould be associated by callingregisterForContextMenu()
And
Pass itView
.If your activity usesListView
OrGridView
And
You want each item to provide the same context menu, register all items for a context menu by passingListView
OrGridView
ToregisterForContextMenu()
.
- Implement
onCreateContextMenu()
Method
In yourActivity
OrFragment
.When the registered view es a long-Click Event, the system callyouronCreateContextMenu()
Method.
This is where you define the menu items, usually by inflating a menu resource. For example:
@Overridepublic 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 includeView
That
The user selected andContextMenu.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:
@Overridepublic 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; default: return super.onContextItemSelected(item); }}
ThegetItemId()
Method
Queries the ID for the selected menu item, which you shoshould assign to each menu item in XML usingandroid:id
Attribute, as shown in the section about defining
A menu in XML.
When you successfully handle a menu item, returntrue
. 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)true
Orfalse
Is
Returned. (the default implementationActivity
Andandroid.app.Fragment
Returnfalse
,
So you shoshould always call the superclass (superclass) When unhandled .)