Resource Types >

Menu Resource

See also

  1. Creating Menus

A menu resource defines an application menu (Options Menu, Context Menu, or Sub Menu) that can be inflated with MenuInflater.

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="@+id/id_name"
          android:menuCategory=["container" | "system" | "secondary" | "alternative"]
          android:orderInCategory="integer"
          android:title="string"
          android:titleCondensed="string"
          android:icon="@[package:]drawable/drawable_resource_name"
          android:alphabeticShortcut="string"
          android:numericShortcut="string"
          android:checkable=["true" | "false"]
          android:visible=["visible" | "invisible" | "gone"]
          android:enabled=["enabled" | "disabled"] />
    <group android:id="resource ID"
           android:menuCategory=["container" | "system" | "secondary" | "alternative"]
           android:orderInCategory="integer"
           android:checkableBehavior=["none" | "all" | "single"]
           android:visible=["visible" | "invisible" | "gone"]
           android:enabled=["enabled" | "disabled"] >
        <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
String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".
<group>
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 <item> elements. Must be a child of a <menu> element.

attributes:

android:id
Resource name. A unique resource name. The value takes the form: "@+id/name".
android:menuCategory
Keyword. Value corresponding to Menu CATEGORY_* constants, which define the group's priority. Valid values:
ValueDescription
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.
android:checkableBehavior
Keyword. The type of checkable behavior for the group. Valid values:
ValueDescription
noneNot checkable
allAll items can be checked (use checkboxes)
singleOnly one item can be checked (use radio buttons)
android:visible
Boolean. "true" if the group is visible.
android:enabled
Boolean. "true" if the group is enabled.
<item>
A menu item. May contain a <menu> element (for a Sub Menu). Must be a child of a <menu> or <group> element.

attributes:

android:id
Resource name. A unique resource name. The value takes the form: "@+id/name".
android:menuCategory
Keyword. Value corresponding to Menu CATEGORY_* constants, which define the item's priority. Valid values:
ValueDescription
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.
android:title
String. The menu title.
android:titleCondensed
String. A condensed title, 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:alphabeticShortcut
Char. A character for the alphabetic shortcut key.
android:numericShortcut
Integer. A number for the numeric shortcut key.
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.
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" />
    <group android:id="@+id/group">
        <item android:id="@+id/group_item1"
              android:title="@string/group_item1"
              android:icon="@drawable/group_item1_icon" />
        <item android:id="@+id/group_item2"
              android:title="G@string/group_item2"
              android:icon="@drawable/group_item2_icon" />
    </group>
    <item android:id="@+id/submenu"
          android:title="@string/submenu_title" >
        <menu>
            <item android:id="@+id/submenu_item1"
                  android:title="@string/submenu_item1" />
        </menu>
    </item>
</menu>

This application code will inflate the menu from the onCreateOptionsMenu(Menu) callback:

public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.example_menu, menu);
  return true;
}
↑ Go to top

← Back to Resource Types