See also
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:
-
res/menu/ filename .xml
The filename will be 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:
-
<?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:
- example:
-
XML file saved at
res/menu/example_menu.xml
:<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>
The following application code inflates the menu from the
onCreateOptionsMenu(Menu)
callback and also declares the on-click callback for two of the items:public 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()
}Note: The
android:showAsAction
attribute is available only on Android 3.0 (API Level 11) and greater.