2010-02-09 18:48:27 -08:00
|
|
|
page.title=Menu Resource
|
|
|
|
parent.title=Resource Types
|
|
|
|
parent.link=available-resources.html
|
|
|
|
@jd:body
|
|
|
|
|
|
|
|
<div id="qv-wrapper">
|
|
|
|
<div id="qv">
|
|
|
|
<h2>See also</h2>
|
|
|
|
<ol>
|
|
|
|
<li><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a></li>
|
|
|
|
</ol>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2011-02-02 20:08:20 -08:00
|
|
|
<p>A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that
|
2010-02-09 18:48:27 -08:00
|
|
|
can be inflated with {@link android.view.MenuInflater}.</p>
|
|
|
|
|
2011-02-02 20:08:20 -08:00
|
|
|
<p>For a guide to using menus, see the <a href="{@docRoot}guide/topics/ui/menus.html">Creating
|
|
|
|
Menus</a> document.</p>
|
|
|
|
|
2010-02-09 18:48:27 -08:00
|
|
|
<dl class="xml">
|
|
|
|
|
|
|
|
<dt>file location:</dt>
|
|
|
|
<dd><code>res/menu/<em>filename</em>.xml</code><br/>
|
|
|
|
The filename will be used as the resource ID.</dd>
|
|
|
|
|
|
|
|
<dt>compiled resource datatype:</dt>
|
|
|
|
<dd>Resource pointer to a {@link android.view.Menu} (or subclass) resource.</dd>
|
|
|
|
|
|
|
|
<dt>resource reference:</dt>
|
|
|
|
<dd>
|
|
|
|
In Java: <code>R.menu.<em>filename</em></code><br/>
|
|
|
|
In XML: <code>@[<em>package</em>:]menu.<em>filename</em></code>
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>syntax:</dt>
|
|
|
|
<dd>
|
|
|
|
<pre>
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<<a href="#menu-element">menu</a> xmlns:android="http://schemas.android.com/apk/res/android">
|
2010-06-08 15:54:52 -07:00
|
|
|
<<a href="#item-element">item</a> android:id="@[+][<em>package</em>:]id/<em>resource_name</em>"
|
2010-02-09 18:48:27 -08:00
|
|
|
android:title="<em>string</em>"
|
|
|
|
android:titleCondensed="<em>string</em>"
|
2010-04-09 15:52:18 -07:00
|
|
|
android:icon="@[package:]drawable/<em>drawable_resource_name</em>"
|
2010-11-29 16:45:49 -08:00
|
|
|
android:onClick="<em>method name</em>"
|
2010-11-16 22:52:09 -08:00
|
|
|
android:showAsAction=["ifRoom" | "never" | "withText" | "always"]
|
2010-11-29 16:45:49 -08:00
|
|
|
android:actionLayout="@[package:]layout/<em>layout_resource_name</em>"
|
|
|
|
android:actionViewClass="<em>class name</em>"
|
2010-02-09 18:48:27 -08:00
|
|
|
android:alphabeticShortcut="<em>string</em>"
|
|
|
|
android:numericShortcut="<em>string</em>"
|
|
|
|
android:checkable=["true" | "false"]
|
2011-05-04 14:33:46 -07:00
|
|
|
android:visible=["true" | "false"]
|
2011-05-04 14:36:56 -07:00
|
|
|
android:enabled=["true" | "false"]
|
2010-11-16 22:52:09 -08:00
|
|
|
android:menuCategory=["container" | "system" | "secondary" | "alternative"]
|
|
|
|
android:orderInCategory="<em>integer</em>" />
|
2010-06-08 15:54:52 -07:00
|
|
|
<<a href="#group-element">group</a> android:id="@[+][<em>package</em>:]id/<em>resource name</em>"
|
2010-02-09 18:48:27 -08:00
|
|
|
android:checkableBehavior=["none" | "all" | "single"]
|
2011-05-04 14:33:46 -07:00
|
|
|
android:visible=["true" | "false"]
|
2011-05-04 14:36:56 -07:00
|
|
|
android:enabled=["true" | "false"]
|
2010-11-16 22:52:09 -08:00
|
|
|
android:menuCategory=["container" | "system" | "secondary" | "alternative"]
|
|
|
|
android:orderInCategory="<em>integer</em>" >
|
2010-02-09 18:48:27 -08:00
|
|
|
<<a href="#item-element">item</a> />
|
|
|
|
</group>
|
|
|
|
<<a href="#item-element">item</a> >
|
|
|
|
<<a href="#menu-element">menu</a>>
|
|
|
|
<<a href="#item-element">item</a> />
|
|
|
|
</menu>
|
|
|
|
</item>
|
|
|
|
</menu>
|
|
|
|
</pre>
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>elements:</dt>
|
|
|
|
<dd>
|
|
|
|
<dl class="tag-list">
|
|
|
|
|
|
|
|
<dt id="menu-element"><code><menu></code></dt>
|
|
|
|
<dd><strong>Required.</strong> This must be the root node. Contains <code><item></code> and/or
|
|
|
|
<code><group></code> elements.
|
|
|
|
<p class="caps">attributes:</p>
|
|
|
|
<dl class="atn-list">
|
|
|
|
<dt><code>xmlns:android</code></dt>
|
2010-11-29 16:45:49 -08:00
|
|
|
<dd><em>XML namespace</em>. <strong>Required.</strong> Defines the XML namespace, which
|
|
|
|
must be <code>"http://schemas.android.com/apk/res/android"</code>.
|
2010-02-09 18:48:27 -08:00
|
|
|
</dl>
|
|
|
|
</dd>
|
2010-11-16 22:52:09 -08:00
|
|
|
|
|
|
|
<dt id="item-element"><code><item></code></dt>
|
|
|
|
<dd>A menu item. May contain a <code><menu></code> element (for a Sub
|
|
|
|
Menu). Must be a child of a <code><menu></code> or <code><group></code> element.
|
2010-02-09 18:48:27 -08:00
|
|
|
<p class="caps">attributes:</p>
|
|
|
|
<dl class="atn-list">
|
|
|
|
<dt><code>android:id</code></dt>
|
2010-06-08 15:54:52 -07:00
|
|
|
<dd><em>Resource ID</em>. A unique resource ID. To create a new resource ID for this item, use the form:
|
2010-11-16 22:52:09 -08:00
|
|
|
<code>"@+id/<em>name</em>"</code>. The plus symbol indicates that this should be created as a new
|
|
|
|
ID.</dd>
|
|
|
|
<dt><code>android:title</code></dt>
|
2010-11-29 16:45:49 -08:00
|
|
|
<dd><em>String resource</em>. The menu title as a string resource or raw string.</dd>
|
2010-11-16 22:52:09 -08:00
|
|
|
<dt><code>android:titleCondensed</code></dt>
|
2010-11-29 16:45:49 -08:00
|
|
|
<dd><em>String resource</em>. 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.</dd>
|
|
|
|
|
2010-11-16 22:52:09 -08:00
|
|
|
<dt><code>android:icon</code></dt>
|
|
|
|
<dd><em>Drawable resource</em>. An image to be used as the menu item icon.</dd>
|
|
|
|
|
2010-11-29 16:45:49 -08:00
|
|
|
<dt><code>android:onClick</code></dt>
|
|
|
|
<dd><em>Method name</em>. The method to call when this menu item is clicked. The
|
|
|
|
method must be declared in the activity as public and accept a {@link android.view.MenuItem} as its
|
|
|
|
only parameter, which indicates the item clicked. This method takes precedence over the standard
|
|
|
|
callback to {@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()}. See the
|
|
|
|
example at the bottom.
|
|
|
|
<p class="warning"><strong>Warning:</strong> If you obfuscate your code using <a
|
|
|
|
href="{@docRoot}guide/developing/tools/proguard.html">ProGuard</a> (or a similar tool),
|
|
|
|
be sure to exclude the method you specify in this attribute from renaming, because it can break the
|
|
|
|
functionality.</p>
|
2011-02-02 20:08:20 -08:00
|
|
|
<p>Introduced in API Level 11.</p></dd>
|
2010-11-29 16:45:49 -08:00
|
|
|
|
2010-11-16 22:52:09 -08:00
|
|
|
<dt><code>android:showAsAction</code></dt>
|
|
|
|
<dd><em>Keyword</em>. 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 {@link
|
2011-02-02 20:08:20 -08:00
|
|
|
android.app.ActionBar} (introduced in API Level 11). Valid values:
|
2010-11-16 22:52:09 -08:00
|
|
|
<table>
|
|
|
|
<tr><th>Value</th><th>Description</th></tr>
|
|
|
|
<tr><td><code>ifRoom</code></td><td>Only place this item in the Action Bar if
|
|
|
|
there is room for it.</td></tr>
|
|
|
|
<tr><td><code>withText</code></td><td>Also include the title text (defined
|
|
|
|
by {@code 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 {@code |}.</td></tr>
|
|
|
|
<tr><td><code>never</code></td><td>Never place this item in the Action Bar.</td></tr>
|
|
|
|
<tr><td><code>always</code></td><td>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.</td></tr>
|
|
|
|
</table>
|
|
|
|
<p>See <a href="{@docRoot}guide/topics/ui/actionbar.html">Using the Action Bar</a> for
|
|
|
|
more information.</p>
|
2011-02-02 20:08:20 -08:00
|
|
|
<p>Introduced in API Level 11.</p>
|
2010-11-16 22:52:09 -08:00
|
|
|
</dd>
|
|
|
|
|
2010-11-29 16:45:49 -08:00
|
|
|
<dt><code>android:actionViewLayout</code></dt>
|
|
|
|
<dd><em>Layout resource</em>. A layout to use as the action view.
|
|
|
|
<p>See <a href="{@docRoot}guide/topics/ui/actionbar.html">Using the Action Bar</a> for
|
|
|
|
more information.</p>
|
2011-02-02 20:08:20 -08:00
|
|
|
<p>Introduced in API Level 11.</p></dd>
|
2010-11-29 16:45:49 -08:00
|
|
|
|
|
|
|
<dt><code>android:actionViewClassName</code></dt>
|
|
|
|
<dd><em>Class name</em>. A fully-qualified class name for the {@link android.view.View}
|
|
|
|
to use as the action view.
|
|
|
|
<p>See <a href="{@docRoot}guide/topics/ui/actionbar.html">Using the Action Bar</a> for
|
|
|
|
more information.</p>
|
|
|
|
<p class="warning"><strong>Warning:</strong> If you obfuscate your code using <a
|
|
|
|
href="{@docRoot}guide/developing/tools/proguard.html">ProGuard</a> (or a similar tool),
|
|
|
|
be sure to exclude the class you specify in this attribute from renaming, because it can break the
|
|
|
|
functionality.</p>
|
2011-02-02 20:08:20 -08:00
|
|
|
<p>Introduced in API Level 11.</p></dd>
|
2010-11-29 16:45:49 -08:00
|
|
|
|
|
|
|
|
2010-11-16 22:52:09 -08:00
|
|
|
<dt><code>android:alphabeticShortcut</code></dt>
|
|
|
|
<dd><em>Char</em>. A character for the alphabetic shortcut key.</dd>
|
|
|
|
<dt><code>android:numericShortcut</code></dt>
|
|
|
|
<dd><em>Integer</em>. A number for the numeric shortcut key.</dd>
|
|
|
|
<dt><code>android:checkable</code></dt>
|
|
|
|
<dd><em>Boolean</em>. "true" if the item is checkable.</dd>
|
|
|
|
<dt><code>android:checked</code></dt>
|
|
|
|
<dd><em>Boolean</em>. "true" if the item is checked by default.</dd>
|
|
|
|
<dt><code>android:visible</code></dt>
|
|
|
|
<dd><em>Boolean</em>. "true" if the item is visible by default.</dd>
|
|
|
|
<dt><code>android:enabled</code></dt>
|
|
|
|
<dd><em>Boolean</em>. "true" if the item is enabled by default.</dd>
|
2010-02-09 18:48:27 -08:00
|
|
|
<dt><code>android:menuCategory</code></dt>
|
|
|
|
<dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*}
|
2010-11-16 22:52:09 -08:00
|
|
|
constants, which define the item's priority. Valid values:
|
2010-02-09 18:48:27 -08:00
|
|
|
<table>
|
|
|
|
<tr><th>Value</th><th>Description</th></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>container</code></td><td>For items that are part of a
|
2010-02-09 18:48:27 -08:00
|
|
|
container.</td></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>system</code></td><td>For items that are provided by the
|
2010-02-09 18:48:27 -08:00
|
|
|
system.</td></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>secondary</code></td><td>For items that are user-supplied secondary
|
2010-02-09 18:48:27 -08:00
|
|
|
(infrequently used) options.</td></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>alternative</code></td><td>For items that are alternative actions
|
2010-02-09 18:48:27 -08:00
|
|
|
on the data that is currently displayed.</td></tr>
|
|
|
|
</table>
|
|
|
|
</dd>
|
|
|
|
<dt><code>android:orderInCategory</code></dt>
|
2010-11-16 22:52:09 -08:00
|
|
|
<dd><em>Integer</em>. The order of "importance" of the item, within a group.</dd>
|
|
|
|
</dl>
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
<dt id="group-element"><code><group></code></dt>
|
|
|
|
<dd>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 <code><item></code> elements. Must be a
|
|
|
|
child of a <code><menu></code> element.
|
|
|
|
<p class="caps">attributes:</p>
|
|
|
|
<dl class="atn-list">
|
|
|
|
<dt><code>android:id</code></dt>
|
|
|
|
<dd><em>Resource ID</em>. A unique resource ID. To create a new resource ID for this item,
|
|
|
|
use the form:
|
|
|
|
<code>"@+id/<em>name</em>"</code>. The plus symbol indicates that this should be created as a new
|
|
|
|
ID.</dd>
|
2010-02-09 18:48:27 -08:00
|
|
|
<dt><code>android:checkableBehavior</code></dt>
|
|
|
|
<dd><em>Keyword</em>. The type of checkable behavior for the group. Valid values:
|
|
|
|
<table>
|
|
|
|
<tr><th>Value</th><th>Description</th></tr>
|
|
|
|
<tr><td><code>none</code></td><td>Not checkable</td></tr>
|
|
|
|
<tr><td><code>all</code></td><td>All items can be checked (use checkboxes)</td></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>single</code></td><td>Only one item can be checked (use radio
|
|
|
|
buttons)</td></tr>
|
2010-02-09 18:48:27 -08:00
|
|
|
</table>
|
|
|
|
</dd>
|
|
|
|
<dt><code>android:visible</code></dt>
|
|
|
|
<dd><em>Boolean</em>. "true" if the group is visible.</dd>
|
|
|
|
<dt><code>android:enabled</code></dt>
|
|
|
|
<dd><em>Boolean</em>. "true" if the group is enabled.</dd>
|
|
|
|
<dt><code>android:menuCategory</code></dt>
|
|
|
|
<dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*}
|
2010-11-16 22:52:09 -08:00
|
|
|
constants, which define the group's priority. Valid values:
|
2010-02-09 18:48:27 -08:00
|
|
|
<table>
|
|
|
|
<tr><th>Value</th><th>Description</th></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>container</code></td><td>For groups that are part of a
|
2010-02-09 18:48:27 -08:00
|
|
|
container.</td></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>system</code></td><td>For groups that are provided by the
|
2010-02-09 18:48:27 -08:00
|
|
|
system.</td></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>secondary</code></td><td>For groups that are user-supplied secondary
|
2010-02-09 18:48:27 -08:00
|
|
|
(infrequently used) options.</td></tr>
|
2010-11-16 22:52:09 -08:00
|
|
|
<tr><td><code>alternative</code></td><td>For groups that are alternative actions
|
2010-02-09 18:48:27 -08:00
|
|
|
on the data that is currently displayed.</td></tr>
|
|
|
|
</table>
|
|
|
|
</dd>
|
|
|
|
<dt><code>android:orderInCategory</code></dt>
|
2010-11-16 22:52:09 -08:00
|
|
|
<dd><em>Integer</em>. The default order of the items within the category.</dd>
|
2010-02-09 18:48:27 -08:00
|
|
|
</dl>
|
|
|
|
</dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>example:</dt>
|
|
|
|
<dd>XML file saved at <code>res/menu/example_menu.xml</code>:
|
|
|
|
<pre>
|
|
|
|
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
2010-04-09 15:52:18 -07:00
|
|
|
<item android:id="@+id/item1"
|
|
|
|
android:title="@string/item1"
|
2010-11-16 22:52:09 -08:00
|
|
|
android:icon="@drawable/group_item1_icon"
|
|
|
|
android:showAsAction="ifRoom|withText"/>
|
2010-04-09 15:52:18 -07:00
|
|
|
<group android:id="@+id/group">
|
2010-02-09 18:48:27 -08:00
|
|
|
<item android:id="@+id/group_item1"
|
2010-11-29 16:45:49 -08:00
|
|
|
android:onClick="onGroupItemClick"
|
2010-04-09 15:52:18 -07:00
|
|
|
android:title="@string/group_item1"
|
|
|
|
android:icon="@drawable/group_item1_icon" />
|
2010-02-09 18:48:27 -08:00
|
|
|
<item android:id="@+id/group_item2"
|
2010-11-29 16:45:49 -08:00
|
|
|
android:onClick="onGroupItemClick"
|
2011-03-08 16:32:09 -08:00
|
|
|
android:title="@string/group_item2"
|
2010-04-09 15:52:18 -07:00
|
|
|
android:icon="@drawable/group_item2_icon" />
|
2010-02-09 18:48:27 -08:00
|
|
|
</group>
|
|
|
|
<item android:id="@+id/submenu"
|
2010-11-16 22:52:09 -08:00
|
|
|
android:title="@string/submenu_title"
|
|
|
|
android:showAsAction="ifRoom|withText" >
|
2010-02-09 18:48:27 -08:00
|
|
|
<menu>
|
2010-04-09 15:52:18 -07:00
|
|
|
<item android:id="@+id/submenu_item1"
|
|
|
|
android:title="@string/submenu_item1" />
|
2010-02-09 18:48:27 -08:00
|
|
|
</menu>
|
|
|
|
</item>
|
|
|
|
</menu>
|
|
|
|
</pre>
|
2010-11-29 16:45:49 -08:00
|
|
|
<p>The following application code inflates the menu from the {@link
|
|
|
|
android.app.Activity#onCreateOptionsMenu(Menu)} callback and also declares the on-click
|
|
|
|
callback for two of the items:</p>
|
2010-02-09 18:48:27 -08:00
|
|
|
<pre>
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2010-11-29 16:45:49 -08:00
|
|
|
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 {@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()}
|
2010-02-09 18:48:27 -08:00
|
|
|
}
|
|
|
|
</pre>
|
2010-11-16 22:52:09 -08:00
|
|
|
<p class="note"><strong>Note:</strong> The {@code android:showAsAction} attribute is
|
2011-02-02 20:08:20 -08:00
|
|
|
available only on Android 3.0 (API Level 11) and greater.</p>
|
2010-02-09 18:48:27 -08:00
|
|
|
</dd> <!-- end example -->
|
|
|
|
|
|
|
|
|
|
|
|
</dl>
|