Menu Resource
SEE ALSO
- Menus
A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that can be inflated with
MenuInflater
.
For a guide to using menus, see the Menus developer guide.
- FILE LOCATION:
-
The filename will be used as the resource ID.res/menu/filename.xml
COMPILED RESOURCE DATATYPE: - Resource pointer to a
(or subclass) resource.Menu
RESOURCE REFERENCE: - In Java:
In XML:R.menu.filename
@[package:]menu.filename
SYNTAX: -
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@[+][package:]id/resource_name" android:title="string" android:titleCondensed="string" android:icon="@[package:]drawable/drawable_resource_name" android:onClick="method name" android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"] android:actionLayout="@[package:]layout/layout_resource_name" android:actionViewClass="class name" android:actionProviderClass="class name" android:alphabeticShortcut="string" android:numericShortcut="string" android:checkable=["true" | "false"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" /> <group android:id="@[+][package:]id/resource name" android:checkableBehavior=["none" | "all" | "single"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" > <item /> </group> <item > <menu> <item /> </menu> </item> </menu>
ELEMENTS: -
- Required. This must be the root node. Contains
and/or<item>
<group>
elements.
attributes:
- XML namespace. Required. Defines the XML namespace, which must be
."http://schemas.android.com/apk/res/android"
xmlns:android
- XML namespace. Required. Defines the XML namespace, which must be
- A menu item. May contain a
element (for a Sub Menu). Must be a child of a<menu>
or<menu>
<group>
element.
attributes:
- Resource ID. A unique resource ID. To create a new resource ID for this item, use the form:
. The plus symbol indicates that this should be created as a new ID."@+id/name"
- String resource. The menu title as a string resource or raw string.
- String resource. A condensed title as a string resource or a raw string. This title is used for situations in which the normal title is too long.
- Drawable resource. An image to be used as the menu item icon.
- Method name. The method to call when this menu item is clicked. The method must be declared in the activity as public and accept a
as its only parameter, which indicates the item clicked. This method takes precedence over the standard callback toMenuItem
onOptionsItemSelected()
. See the example at the bottom.
Warning: If you obfuscate your code using ProGuard (or a similar tool), be sure to exclude the method you specify in this attribute from renaming, because it can break the functionality.
Introduced in API Level 11.
- Keyword. When and how this item should appear as an action item in the Action Bar. A menu item can appear as an action item only when the activity includes an
(introduced in API Level 11). Valid values:ActionBar
Value Description ifRoom
Only place this item in the Action Bar if there is room for it. withText
Also include the title text (defined by
) with the action item. You can include this value along with one of the others as a flag set, by separating them with a pipeandroid:title
.|
never
Never place this item in the Action Bar. always
Always place this item in the Action Bar. Avoid using this unless it's critical that the item always appear in the action bar. Setting multiple items to always appear as action items can result in them overlapping with other UI in the action bar. collapseActionView
The action view associated with this action item (as declared by
orandroid:actionLayout
android:actionViewClass
) is collapsible.
Introduced in API Level 14.
See the Action Bar developer guide for more information.
Introduced in API Level 11.
-
Layout resource. A layout to use as the action view.
See the Action Bar developer guide for more information.
Introduced in API Level 11.
- Class name. A fully-qualified class name for the
to use as the action view. For example,View
to use"android.widget.SearchView"
SearchView
as an action view.
See the Action Bar developer guide for more information.
Warning: If you obfuscate your code using ProGuard (or a similar tool), be sure to exclude the class you specify in this attribute from renaming, because it can break the functionality.
Introduced in API Level 11.
- Class name. A fully-qualified class name for the
to use in place of the action item. For example,ActionProvider
to use"android.widget.ShareActionProvider"
ShareActionProvider
.
See the Action Bar developer guide for more information.
Warning: If you obfuscate your code using ProGuard (or a similar tool), be sure to exclude the class you specify in this attribute from renaming, because it can break the functionality.
Introduced in API Level 14.
- Char. A character for the alphabetic shortcut key.
- Integer. A number for the numeric shortcut key.
- Boolean. "true" if the item is checkable.
- Boolean. "true" if the item is checked by default.
- Boolean. "true" if the item is visible by default.
- Boolean. "true" if the item is enabled by default.
- Keyword. Value corresponding to
Menu
constants, which define the item's priority. Valid values:CATEGORY_*
Value Description container
For items that are part of a container. system
For items that are provided by the system. secondary
For items that are user-supplied secondary (infrequently used) options. alternative
For items that are alternative actions on the data that is currently displayed. - Integer. The order of "importance" of the item, within a group.
android:id
android:title
android:titleCondensed
android:icon
android:onClick
android:showAsAction
android:actionLayout
android:actionViewClass
android:actionProviderClass
android:alphabeticShortcut
android:numericShortcut
android:checkable
android:checked
android:visible
android:enabled
android:menuCategory
android:orderInCategory
- Resource ID. A unique resource ID. To create a new resource ID for this item, use the form:
- A menu group (to create a collection of items that share traits, such as whether they are visible, enabled, or checkable). Contains one or more
elements. Must be a child of a<item>
<menu>
element.
attributes:
- Resource ID. A unique resource ID. To create a new resource ID for this item, use the form:
. The plus symbol indicates that this should be created as a new ID."@+id/name"
- Keyword. The type of checkable behavior for the group. Valid values:
Value Description none
Not checkable all
All items can be checked (use checkboxes) single
Only one item can be checked (use radio buttons) - Boolean. "true" if the group is visible.
- Boolean. "true" if the group is enabled.
- Keyword. Value corresponding to
Menu
constants, which define the group's priority. Valid values:CATEGORY_*
Value Description container
For groups that are part of a container. system
For groups that are provided by the system. secondary
For groups that are user-supplied secondary (infrequently used) options. alternative
For groups that are alternative actions on the data that is currently displayed. - Integer. The default order of the items within the category.
android:id
android:checkableBehavior
android:visible
android:enabled
android:menuCategory
android:orderInCategory
- Resource ID. A unique resource ID. To create a new resource ID for this item, use the form:
<menu>
<item>
<group>
EXAMPLE: - Required. This must be the root node. Contains
- XML file saved at
:res/menu/example_menu.xml
The following application code inflates the menu from the<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="@string/item1" android:icon="@drawable/group_item1_icon" android:showAsAction="ifRoom|withText"/> <group android:id="@+id/group"> <item android:id="@+id/group_item1" android:onClick="onGroupItemClick" android:title="@string/group_item1" android:icon="@drawable/group_item1_icon" /> <item android:id="@+id/group_item2" android:onClick="onGroupItemClick" android:title="@string/group_item2" android:icon="@drawable/group_item2_icon" /> </group> <item android:id="@+id/submenu" android:title="@string/submenu_title" android:showAsAction="ifRoom|withText" > <menu> <item android:id="@+id/submenu_item1" android:title="@string/submenu_item1" /> </menu> </item> </menu>
callback and also declares the on-click callback for two of the items:onCreateOptionsMenu(Menu)
Note: Thepublic boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.example_menu, menu); return true; } public void onGroupItemClick(MenuItem item) { // One of the group items (using the onClick attribute) was clicked // The item parameter passed here indicates which item it is // All other menu item clicks are handled by onOptionsItemSelected() }
attribute is available only on Android 3.0 (API Level 11) and greater.android:showAsAction