Menu resource
Stay organized with collections
Save and categorize content based on your preferences.
A menu resource defines an application menu—an options menu, context menu, or submenu—that
can be inflated with MenuInflater .
For a guide to using menus, see Add menus.
- file location:
res/menu/filename.xml
The filename is used as the resource ID- compiled resource datatype:
- Resource pointer to a
Menu(or subclass) resource - resource reference:
-
In Java:
R.menu.filename
In XML:@[package:]menu.filename - syntax:
-
<?xmlversion="1.0"encoding="utf-8"?> <menuxmlns:android="http://schemas.android.com/apk/res/android"> <itemandroid:id="@[+][package:]id/resource_name" android:title="string" android:titleCondensed="string" android:icon="@[package:]drawable/drawable_resource_name" android:onClick="methodname" android:showAsAction=["ifRoom"|"never"|"withText"|"always"|"collapseActionView"] android:actionLayout="@[package:]layout/layout_resource_name" android:actionViewClass="classname" android:actionProviderClass="classname" android:alphabeticShortcut="string" android:alphabeticModifiers=["META"|"CTRL"|"ALT"|"SHIFT"|"SYM"|"FUNCTION"] android:numericShortcut="string" android:numericModifiers=["META"|"CTRL"|"ALT"|"SHIFT"|"SYM"|"FUNCTION"] android:checkable=["true"|"false"] android:visible=["true"|"false"] android:enabled=["true"|"false"] android:menuCategory=["container"|"system"|"secondary"|"alternative"] android:orderInCategory="integer"/> <groupandroid:id="@[+][package:]id/resourcename" 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
<item>and/or<group>elements.Attributes:
xmlns:android- XML namespace. Required. Defines the XML namespace, which
must be
"http://schemas.android.com/apk/res/android".
<item>- A menu item. Might contain a
<menu>element (for a submenu). Must be a child of a<menu>or<group>element.Attributes:
android:id- Resource ID. A unique resource ID. To create a new resource ID for this item, use the form:
"@+id/name". The plus symbol indicates that this is created as a new ID. android:title- String resource. The menu title as a string resource or raw string.
android:titleCondensed- 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.
android:icon- Drawable resource. An image to be used as the menu item icon.
android:onClick- Method name. The method to call when this menu item is clicked. The
method must be declared in the activity as public. It accepts a
MenuItemas its only parameter, which indicates the item clicked. This method takes precedence over the standard callback toonOptionsItemSelected(). See the example at the end of this page.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.
android:showAsAction- Keyword. When and how this item appears as an action item in the app
bar. A menu item can appear as an action item only when the activity includes an
app bar. Valid values:
Value Description ifRoomOnly place this item in the app bar if there is room for it. If there isn't room for all the items marked "ifRoom", the items with the lowestorderInCategoryvalues are displayed as actions, and the remaining items are displayed in the overflow menu.withTextAlso include the title text (defined by android:title) with the action item. You can include this value along with one of the others as a flag set by separating them with a pipe|.neverNever place this item in the app bar. Instead, list the item in the app bar's overflow menu. alwaysAlways place this item in the app 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 app bar. collapseActionViewThe action view associated with this action item (as declared by android:actionLayoutorandroid:actionViewClass) is collapsible.
Introduced in API level 14.See Add the app bar for more information.
Introduced in API level 11.
android:actionLayout- Layout resource. A layout to use as the action view.
For more information, see Use action views and action providers.
Introduced in API level 11.
android:actionViewClass- Class name. A fully-qualified class name for the
Viewto use as the action view. For example,"android.widget.SearchView"to useSearchViewas an action view.For more information, see Use action views and action providers.
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.
android:actionProviderClass- Class name. A fully qualified class name for the
ActionProviderto use in place of the action item. For example,"android.widget.ShareActionProvider"to useShareActionProvider.For more information, see Use action views and action providers.
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.
android:alphabeticShortcut- Char. A character for the alphabetic shortcut key.
android:numericShortcut- Integer. A number for the numeric shortcut key.
android:alphabeticModifiers- Keyword. A modifier for the menu item's alphabetic
shortcut. The default value corresponds to the Control
key. Valid values:
Value Description METACorresponds to the Meta meta key. CTRL Corresponds to the Control meta key. ALT Corresponds to the Alt meta key. SHIFT Corresponds to the Shift meta key. SYM Corresponds to the Sym meta key. FUNCTION Corresponds to the Function meta key. Note: You can specify multiple keywords in an attribute. For example,
android:alphabeticModifiers="CTRL|SHIFT"indicates that to trigger the corresponding menu item, the user needs to press both the Control and Shift meta keys along with the shortcut.You can use the
setAlphabeticShortcut()method to set the attribute values programmatically. For more information about thealphabeticModifierattribute, seealphabeticModifiers. android:numericModifiers- Keyword. A modifier for the menu item's numeric shortcut.
The default value corresponds to the Control key. Valid
values:
Value Description META Corresponds to the Meta meta key. CTRL Corresponds to the Control meta key. ALT Corresponds to the Alt meta key. SHIFT Corresponds to the Shift meta key. SYM Corresponds to the Sym meta key. FUNCTION Corresponds to the Function meta key. Note: You can specify multiple keywords in an attribute. For example,
android:numericModifiers="CTRL|SHIFT"indicates that to trigger the corresponding menu item, the user needs to press both the Control and Shift meta keys along with the shortcut.You can use the
setNumericShortcut()method to set the attribute values programmatically. For more information about thenumericModifierattribute, seenumericModifiers. android:checkable- Boolean. True if the item is checkable.
android:checked- Boolean. True if the item is checked by default.
android:visible- Boolean. True if the item is visible by default.
android:enabled- Boolean. True if the item is enabled by default.
android:menuCategory- Keyword. Value corresponding to the
MenuCATEGORY_*constants, which define the item's priority. Valid values:Value Description containerFor items that are part of a container. systemFor items that are provided by the system. secondaryFor items that are user-supplied secondary (infrequently used) options. alternativeFor items that are alternative actions on the data that is currently displayed. android:orderInCategory- Integer. The order of importance of the item within a group.
<group>- A menu group, to create a collection of items that share traits, such as whether they are
visible, enabled, or selectable. Contains one or more
<item>elements. Must be a child of a<menu>element.Attributes:
android:id- Resource ID. A unique resource ID. To create a new resource ID for this item,
use the form:
"@+id/name". The plus symbol indicates that this is created as a new ID. android:checkableBehavior- Keyword. The type of selectable behavior for the group. Valid values:
Value Description noneNot selectable. allAll items can be selected (use checkboxes). singleOnly one item can be selected (use radio buttons). android:visible- Boolean. True if the group is visible.
android:enabled- Boolean. True if the group is enabled.
android:menuCategory- Keyword. Value corresponding to the
MenuCATEGORY_*constants, which define the group's priority. Valid values:Value Description containerFor groups that are part of a container. systemFor groups that are provided by the system. secondaryFor groups that are user-supplied secondary (infrequently used) options. alternativeFor groups that are alternative actions on the data that is currently displayed. android:orderInCategory- Integer. The default order of the items within the category.
- Required. This must be the root node. Contains
- example:
- XML file saved at
res/menu/example_menu.xml:<menuxmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <itemandroid:id="@+id/item1" android:title="@string/item1" android:icon="@drawable/group_item1_icon" app:showAsAction="ifRoom|withText"/> <groupandroid:id="@+id/group"> <itemandroid:id="@+id/group_item1" android:onClick="onGroupItemClick" android:title="@string/group_item1" android:icon="@drawable/group_item1_icon"/> <itemandroid:id="@+id/group_item2" android:onClick="onGroupItemClick" android:title="@string/group_item2" android:icon="@drawable/group_item2_icon"/> </group> <itemandroid:id="@+id/submenu" android:title="@string/submenu_title" app:showAsAction="ifRoom|withText"> <menu> <itemandroid:id="@+id/submenu_item1" android:title="@string/submenu_item1"/> </menu> </item> </menu>
The following application code inflates the menu from the
onCreateOptionsMenu(Menu)callback and also declares the on-click callback for two of the items:Kotlin
overridefunonCreateOptionsMenu(menu:Menu):Boolean{ menuInflater.inflate(R.menu.example_menu,menu) returntrue } funonGroupItemClick(item:MenuItem){ // 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 Activity.onOptionsItemSelected. }
Java
publicbooleanonCreateOptionsMenu(Menumenu){ MenuInflaterinflater=getMenuInflater(); inflater.inflate(R.menu.example_menu,menu); returntrue; } publicvoidonGroupItemClick(MenuItemitem){ // 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 Activity.onOptionsItemSelected. }