• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Menu Resource
2parent.title=Resource Types
3parent.link=available-resources.html
4@jd:body
5
6<div id="qv-wrapper">
7  <div id="qv">
8    <h2>See also</h2>
9    <ol>
10      <li><a href="{@docRoot}guide/topics/ui/menus.html">Menus</a></li>
11    </ol>
12  </div>
13</div>
14
15<p>A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that
16can be inflated with {@link android.view.MenuInflater}.</p>
17
18<p>For a guide to using menus, see the <a href="{@docRoot}guide/topics/ui/menus.html">Menus</a>
19developer guide.</p>
20
21<dl class="xml">
22
23<dt>file location:</dt>
24<dd><code>res/menu/<em>filename</em>.xml</code><br/>
25The filename will be used as the resource ID.</dd>
26
27<dt>compiled resource datatype:</dt>
28<dd>Resource pointer to a {@link android.view.Menu} (or subclass) resource.</dd>
29
30<dt>resource reference:</dt>
31<dd>
32In Java: <code>R.menu.<em>filename</em></code><br/>
33In XML: <code>@[<em>package</em>:]menu.<em>filename</em></code>
34</dd>
35
36<dt>syntax:</dt>
37<dd>
38<pre>
39&lt;?xml version="1.0" encoding="utf-8"?>
40&lt;<a href="#menu-element">menu</a> xmlns:android="http://schemas.android.com/apk/res/android">
41    &lt;<a href="#item-element">item</a> android:id="@[+][<em>package</em>:]id/<em>resource_name</em>"
42          android:title="<em>string</em>"
43          android:titleCondensed="<em>string</em>"
44          android:icon="@[package:]drawable/<em>drawable_resource_name</em>"
45          android:onClick="<em>method name</em>"
46          android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]
47          android:actionLayout="@[package:]layout/<em>layout_resource_name</em>"
48          android:actionViewClass="<em>class name</em>"
49          android:actionProviderClass="<em>class name</em>"
50          android:alphabeticShortcut="<em>string</em>"
51          android:numericShortcut="<em>string</em>"
52          android:checkable=["true" | "false"]
53          android:visible=["true" | "false"]
54          android:enabled=["true" | "false"]
55          android:menuCategory=["container" | "system" | "secondary" | "alternative"]
56          android:orderInCategory="<em>integer</em>" /&gt;
57    &lt;<a href="#group-element">group</a> android:id="@[+][<em>package</em>:]id/<em>resource name</em>"
58           android:checkableBehavior=["none" | "all" | "single"]
59           android:visible=["true" | "false"]
60           android:enabled=["true" | "false"]
61           android:menuCategory=["container" | "system" | "secondary" | "alternative"]
62           android:orderInCategory="<em>integer</em>" &gt;
63        &lt;<a href="#item-element">item</a> /&gt;
64    &lt;/group&gt;
65    &lt;<a href="#item-element">item</a> &gt;
66        &lt;<a href="#menu-element">menu</a>&gt;
67          &lt;<a href="#item-element">item</a> /&gt;
68        &lt;/menu&gt;
69    &lt;/item&gt;
70&lt;/menu&gt;
71</pre>
72</dd>
73
74<dt>elements:</dt>
75<dd>
76<dl class="tag-list">
77
78  <dt id="menu-element"><code>&lt;menu&gt;</code></dt>
79    <dd><strong>Required.</strong> This must be the root node. Contains <code>&lt;item></code> and/or
80      <code>&lt;group></code> elements.
81      <p class="caps">attributes:</p>
82      <dl class="atn-list">
83        <dt><code>xmlns:android</code></dt>
84          <dd><em>XML namespace</em>. <strong>Required.</strong> Defines the XML namespace, which
85must be <code>"http://schemas.android.com/apk/res/android"</code>.
86      </dl>
87    </dd>
88
89  <dt id="item-element"><code>&lt;item&gt;</code></dt>
90    <dd>A menu item. May contain a <code>&lt;menu&gt;</code> element (for a Sub
91      Menu). Must be a child of a <code>&lt;menu&gt;</code> or <code>&lt;group&gt;</code> element.
92      <p class="caps">attributes:</p>
93      <dl class="atn-list">
94        <dt><code>android:id</code></dt>
95        <dd><em>Resource ID</em>. A unique resource ID. To create a new resource ID for this item, use the form:
96<code>"@+id/<em>name</em>"</code>. The plus symbol indicates that this should be created as a new
97ID.</dd>
98        <dt><code>android:title</code></dt>
99          <dd><em>String resource</em>. The menu title as a string resource or raw string.</dd>
100        <dt><code>android:titleCondensed</code></dt>
101          <dd><em>String resource</em>. A condensed title as a string resource or a raw string. This
102title is used for situations in which the normal title is too long.</dd>
103
104        <dt><code>android:icon</code></dt>
105          <dd><em>Drawable resource</em>. An image to be used as the menu item icon.</dd>
106
107        <dt><code>android:onClick</code></dt>
108          <dd><em>Method name</em>. The method to call when this menu item is clicked. The
109method must be declared in the activity as public and accept a {@link android.view.MenuItem} as its
110only parameter, which indicates the item clicked. This method takes precedence over the standard
111callback to {@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()}. See the
112example at the bottom.
113          <p class="warning"><strong>Warning:</strong> If you obfuscate your code using <a
114href="{@docRoot}guide/developing/tools/proguard.html">ProGuard</a> (or a similar tool),
115be sure to exclude the method you specify in this attribute from renaming, because it can break the
116functionality.</p>
117          <p>Introduced in API Level 11.</p></dd>
118
119        <dt><code>android:showAsAction</code></dt>
120          <dd><em>Keyword</em>. When and how this item should appear as an action item in the Action
121Bar. A menu item can appear as an action item only when the activity includes an {@link
122android.app.ActionBar} (introduced in API Level 11). Valid values:
123          <table>
124            <tr><th>Value</th><th>Description</th></tr>
125            <tr><td><code>ifRoom</code></td><td>Only place this item in the Action Bar if
126there is room for it.</td></tr>
127            <tr><td><code>withText</code></td><td>Also include the title text (defined
128by {@code android:title}) with the action item. You can include this value along with one
129of the others as a flag set, by separating them with a pipe {@code |}.</td></tr>
130            <tr><td><code>never</code></td><td>Never place this item in the Action Bar.</td></tr>
131            <tr><td><code>always</code></td><td>Always place this item in the Action Bar.
132Avoid using this unless it's critical that the item always appear in the action
133bar. Setting multiple items to always appear as action items can result in them overlapping
134with other UI in the action bar.</td></tr>
135            <tr><td><code>collapseActionView</code></td><td>The action view associated
136with this action item (as declared by <code>android:actionViewLayout</code>) is
137collapsible.<br/>Introduced in API Level 14.</td></tr>
138          </table>
139          <p>See the <a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> developer
140guide for more information.</p>
141          <p>Introduced in API Level 11.</p>
142        </dd>
143
144        <dt><code>android:actionViewLayout</code></dt>
145          <dd><em>Layout resource</em>. A layout to use as the action view.
146          <p>See the <a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> developer
147guide for more information.</p>
148          <p>Introduced in API Level 11.</p></dd>
149
150        <dt><code>android:actionViewClass</code></dt>
151          <dd><em>Class name</em>. A fully-qualified class name for the {@link android.view.View}
152to use as the action view. For example,
153{@code "android.widget.SearchView"} to use {@link android.widget.SearchView} as an action view.
154          <p>See the <a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> developer
155guide for more information.</p>
156          <p class="warning"><strong>Warning:</strong> If you obfuscate your code using <a
157href="{@docRoot}guide/developing/tools/proguard.html">ProGuard</a> (or a similar tool),
158be sure to exclude the class you specify in this attribute from renaming, because it can break the
159functionality.</p>
160          <p>Introduced in API Level 11.</p></dd>
161
162        <dt><code>android:actionProviderClass</code></dt>
163          <dd><em>Class name</em>. A fully-qualified class name for the {@link
164android.view.ActionProvider} to use in place of the action item. For example,
165{@code "android.widget.ShareActionProvider"} to use {@link android.widget.ShareActionProvider}.
166          <p>See the <a href="{@docRoot}guide/topics/ui/actionbar.html">Action Bar</a> developer
167guide for more information.</p>
168          <p class="warning"><strong>Warning:</strong> If you obfuscate your code using <a
169href="{@docRoot}guide/developing/tools/proguard.html">ProGuard</a> (or a similar tool),
170be sure to exclude the class you specify in this attribute from renaming, because it can break the
171functionality.</p>
172          <p>Introduced in API Level 14.</p></dd>
173
174        <dt><code>android:alphabeticShortcut</code></dt>
175          <dd><em>Char</em>. A character for the alphabetic shortcut key.</dd>
176        <dt><code>android:numericShortcut</code></dt>
177          <dd><em>Integer</em>. A number for the numeric shortcut key.</dd>
178        <dt><code>android:checkable</code></dt>
179          <dd><em>Boolean</em>. "true" if the item is checkable.</dd>
180        <dt><code>android:checked</code></dt>
181          <dd><em>Boolean</em>. "true" if the item is checked by default.</dd>
182        <dt><code>android:visible</code></dt>
183          <dd><em>Boolean</em>. "true" if the item is visible by default.</dd>
184        <dt><code>android:enabled</code></dt>
185          <dd><em>Boolean</em>. "true" if the item is enabled by default.</dd>
186        <dt><code>android:menuCategory</code></dt>
187          <dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*}
188          constants, which define the item's priority. Valid values:
189          <table>
190            <tr><th>Value</th><th>Description</th></tr>
191            <tr><td><code>container</code></td><td>For items that are part of a
192container.</td></tr>
193            <tr><td><code>system</code></td><td>For items that are provided by the
194system.</td></tr>
195            <tr><td><code>secondary</code></td><td>For items that are user-supplied secondary
196(infrequently used) options.</td></tr>
197            <tr><td><code>alternative</code></td><td>For items that are alternative actions
198on the data that is currently displayed.</td></tr>
199          </table>
200        </dd>
201        <dt><code>android:orderInCategory</code></dt>
202          <dd><em>Integer</em>. The order of "importance" of the item, within a group.</dd>
203      </dl>
204    </dd>
205
206  <dt id="group-element"><code>&lt;group&gt;</code></dt>
207    <dd>A menu group (to create a collection of items that share traits, such as whether they are
208visible, enabled, or checkable). Contains one or more <code>&lt;item&gt;</code> elements. Must be a
209child of a <code>&lt;menu&gt;</code> element.
210      <p class="caps">attributes:</p>
211      <dl class="atn-list">
212        <dt><code>android:id</code></dt>
213        <dd><em>Resource ID</em>. A unique resource ID. To create a new resource ID for this item,
214use the form:
215<code>"@+id/<em>name</em>"</code>. The plus symbol indicates that this should be created as a new
216ID.</dd>
217        <dt><code>android:checkableBehavior</code></dt>
218        <dd><em>Keyword</em>. The type of checkable behavior for the group. Valid values:
219          <table>
220            <tr><th>Value</th><th>Description</th></tr>
221            <tr><td><code>none</code></td><td>Not checkable</td></tr>
222            <tr><td><code>all</code></td><td>All items can be checked (use checkboxes)</td></tr>
223            <tr><td><code>single</code></td><td>Only one item can be checked (use radio
224buttons)</td></tr>
225          </table>
226        </dd>
227        <dt><code>android:visible</code></dt>
228        <dd><em>Boolean</em>. "true" if the group is visible.</dd>
229        <dt><code>android:enabled</code></dt>
230        <dd><em>Boolean</em>. "true" if the group is enabled.</dd>
231        <dt><code>android:menuCategory</code></dt>
232          <dd><em>Keyword</em>. Value corresponding to {@link android.view.Menu} {@code CATEGORY_*}
233          constants, which define the group's priority. Valid values:
234          <table>
235            <tr><th>Value</th><th>Description</th></tr>
236            <tr><td><code>container</code></td><td>For groups that are part of a
237container.</td></tr>
238            <tr><td><code>system</code></td><td>For groups that are provided by the
239system.</td></tr>
240            <tr><td><code>secondary</code></td><td>For groups that are user-supplied secondary
241(infrequently used) options.</td></tr>
242            <tr><td><code>alternative</code></td><td>For groups that are alternative actions
243on the data that is currently displayed.</td></tr>
244          </table>
245        </dd>
246        <dt><code>android:orderInCategory</code></dt>
247        <dd><em>Integer</em>. The default order of the items within the category.</dd>
248      </dl>
249    </dd>
250</dl>
251
252</dd>
253
254<dt>example:</dt>
255<dd>XML file saved at <code>res/menu/example_menu.xml</code>:
256<pre>
257&lt;menu xmlns:android="http://schemas.android.com/apk/res/android">
258    &lt;item android:id="@+id/item1"
259          android:title="@string/item1"
260          android:icon="@drawable/group_item1_icon"
261          android:showAsAction="ifRoom|withText"/>
262    &lt;group android:id="@+id/group">
263        &lt;item android:id="@+id/group_item1"
264              android:onClick="onGroupItemClick"
265              android:title="@string/group_item1"
266              android:icon="@drawable/group_item1_icon" />
267        &lt;item android:id="@+id/group_item2"
268              android:onClick="onGroupItemClick"
269              android:title="@string/group_item2"
270              android:icon="@drawable/group_item2_icon" />
271    &lt;/group>
272    &lt;item android:id="@+id/submenu"
273          android:title="@string/submenu_title"
274          android:showAsAction="ifRoom|withText" >
275        &lt;menu>
276            &lt;item android:id="@+id/submenu_item1"
277                  android:title="@string/submenu_item1" />
278        &lt;/menu>
279    &lt;/item>
280&lt;/menu>
281</pre>
282    <p>The following application code inflates the menu from the {@link
283android.app.Activity#onCreateOptionsMenu(Menu)} callback and also declares the on-click
284callback for two of the items:</p>
285<pre>
286public boolean onCreateOptionsMenu(Menu menu) {
287    MenuInflater inflater = getMenuInflater();
288    inflater.inflate(R.menu.example_menu, menu);
289    return true;
290}
291
292public void onGroupItemClick(MenuItem item) {
293    // One of the group items (using the onClick attribute) was clicked
294    // The item parameter passed here indicates which item it is
295    // All other menu item clicks are handled by {@link android.app.Activity#onOptionsItemSelected onOptionsItemSelected()}
296}
297</pre>
298<p class="note"><strong>Note:</strong> The {@code android:showAsAction} attribute is
299available only on Android 3.0 (API Level 11) and greater.</p>
300</dd> <!-- end example -->
301
302
303</dl>