2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.actionbarsherlock
.view
;
19 import android
.content
.ComponentName
;
20 import android
.content
.Intent
;
21 import android
.view
.KeyEvent
;
24 * Interface for managing the items in a menu.
26 * By default, every Activity supports an options menu of actions or options.
27 * You can add items to this menu and handle clicks on your additions. The
28 * easiest way of adding menu items is inflating an XML file into the
29 * {@link Menu} via {@link MenuInflater}. The easiest way of attaching code to
30 * clicks is via {@link Activity#onOptionsItemSelected(MenuItem)} and
31 * {@link Activity#onContextItemSelected(MenuItem)}.
33 * Different menu types support different features:
35 * <li><b>Context menus</b>: Do not support item shortcuts and item icons.
36 * <li><b>Options menus</b>: The <b>icon menus</b> do not support item check
37 * marks and only show the item's
38 * {@link MenuItem#setTitleCondensed(CharSequence) condensed title}. The
39 * <b>expanded menus</b> (only available if six or more menu items are visible,
40 * reached via the 'More' item in the icon menu) do not show item icons, and
41 * item check marks are discouraged.
42 * <li><b>Sub menus</b>: Do not support item icons, or nested sub menus.
45 * <div class="special reference">
46 * <h3>Developer Guides</h3>
47 * <p>For more information about creating menus, read the
48 * <a href="{@docRoot}guide/topics/ui/menus.html">Menus</a> developer guide.</p>
51 public interface Menu
{
54 * This is the part of an order integer that the user can provide.
57 static final int USER_MASK
= 0x0000ffff;
59 * Bit shift of the user portion of the order integer.
62 static final int USER_SHIFT
= 0;
65 * This is the part of an order integer that supplies the category of the
69 static final int CATEGORY_MASK
= 0xffff0000;
71 * Bit shift of the category portion of the order integer.
74 static final int CATEGORY_SHIFT
= 16;
77 * Value to use for group and item identifier integers when you don't care
80 static final int NONE
= 0;
83 * First value for group and item identifier integers.
85 static final int FIRST
= 1;
87 // Implementation note: Keep these CATEGORY_* in sync with the category enum
91 * Category code for the order integer for items/groups that are part of a
92 * container -- or/add this with your base value.
94 static final int CATEGORY_CONTAINER
= 0x00010000;
97 * Category code for the order integer for items/groups that are provided by
98 * the system -- or/add this with your base value.
100 static final int CATEGORY_SYSTEM
= 0x00020000;
103 * Category code for the order integer for items/groups that are
104 * user-supplied secondary (infrequently used) options -- or/add this with
107 static final int CATEGORY_SECONDARY
= 0x00030000;
110 * Category code for the order integer for items/groups that are
111 * alternative actions on the data that is currently displayed -- or/add
112 * this with your base value.
114 static final int CATEGORY_ALTERNATIVE
= 0x00040000;
117 * Flag for {@link #addIntentOptions}: if set, do not automatically remove
118 * any existing menu items in the same group.
120 static final int FLAG_APPEND_TO_GROUP
= 0x0001;
123 * Flag for {@link #performShortcut}: if set, do not close the menu after
124 * executing the shortcut.
126 static final int FLAG_PERFORM_NO_CLOSE
= 0x0001;
129 * Flag for {@link #performShortcut(int, KeyEvent, int)}: if set, always
130 * close the menu after executing the shortcut. Closing the menu also resets
131 * the prepared state.
133 static final int FLAG_ALWAYS_PERFORM_CLOSE
= 0x0002;
136 * Add a new item to the menu. This item displays the given title for its
139 * @param title The text to display for the item.
140 * @return The newly added menu item.
142 public MenuItem
add(CharSequence title
);
145 * Add a new item to the menu. This item displays the given title for its
148 * @param titleRes Resource identifier of title string.
149 * @return The newly added menu item.
151 public MenuItem
add(int titleRes
);
154 * Add a new item to the menu. This item displays the given title for its
157 * @param groupId The group identifier that this item should be part of.
158 * This can be used to define groups of items for batch state
159 * changes. Normally use {@link #NONE} if an item should not be in a
161 * @param itemId Unique item ID. Use {@link #NONE} if you do not need a
163 * @param order The order for the item. Use {@link #NONE} if you do not care
164 * about the order. See {@link MenuItem#getOrder()}.
165 * @param title The text to display for the item.
166 * @return The newly added menu item.
168 public MenuItem
add(int groupId
, int itemId
, int order
, CharSequence title
);
171 * Variation on {@link #add(int, int, int, CharSequence)} that takes a
172 * string resource identifier instead of the string itself.
174 * @param groupId The group identifier that this item should be part of.
175 * This can also be used to define groups of items for batch state
176 * changes. Normally use {@link #NONE} if an item should not be in a
178 * @param itemId Unique item ID. Use {@link #NONE} if you do not need a
180 * @param order The order for the item. Use {@link #NONE} if you do not care
181 * about the order. See {@link MenuItem#getOrder()}.
182 * @param titleRes Resource identifier of title string.
183 * @return The newly added menu item.
185 public MenuItem
add(int groupId
, int itemId
, int order
, int titleRes
);
188 * Add a new sub-menu to the menu. This item displays the given title for
189 * its label. To modify other attributes on the submenu's menu item, use
190 * {@link SubMenu#getItem()}.
192 * @param title The text to display for the item.
193 * @return The newly added sub-menu
195 SubMenu
addSubMenu(final CharSequence title
);
198 * Add a new sub-menu to the menu. This item displays the given title for
199 * its label. To modify other attributes on the submenu's menu item, use
200 * {@link SubMenu#getItem()}.
202 * @param titleRes Resource identifier of title string.
203 * @return The newly added sub-menu
205 SubMenu
addSubMenu(final int titleRes
);
208 * Add a new sub-menu to the menu. This item displays the given
209 * <var>title</var> for its label. To modify other attributes on the
210 * submenu's menu item, use {@link SubMenu#getItem()}.
212 * Note that you can only have one level of sub-menus, i.e. you cannnot add
213 * a subMenu to a subMenu: An {@link UnsupportedOperationException} will be
216 * @param groupId The group identifier that this item should be part of.
217 * This can also be used to define groups of items for batch state
218 * changes. Normally use {@link #NONE} if an item should not be in a
220 * @param itemId Unique item ID. Use {@link #NONE} if you do not need a
222 * @param order The order for the item. Use {@link #NONE} if you do not care
223 * about the order. See {@link MenuItem#getOrder()}.
224 * @param title The text to display for the item.
225 * @return The newly added sub-menu
227 SubMenu
addSubMenu(final int groupId
, final int itemId
, int order
, final CharSequence title
);
230 * Variation on {@link #addSubMenu(int, int, int, CharSequence)} that takes
231 * a string resource identifier for the title instead of the string itself.
233 * @param groupId The group identifier that this item should be part of.
234 * This can also be used to define groups of items for batch state
235 * changes. Normally use {@link #NONE} if an item should not be in a group.
236 * @param itemId Unique item ID. Use {@link #NONE} if you do not need a unique ID.
237 * @param order The order for the item. Use {@link #NONE} if you do not care about the
238 * order. See {@link MenuItem#getOrder()}.
239 * @param titleRes Resource identifier of title string.
240 * @return The newly added sub-menu
242 SubMenu
addSubMenu(int groupId
, int itemId
, int order
, int titleRes
);
245 * Add a group of menu items corresponding to actions that can be performed
246 * for a particular Intent. The Intent is most often configured with a null
247 * action, the data that the current activity is working with, and includes
248 * either the {@link Intent#CATEGORY_ALTERNATIVE} or
249 * {@link Intent#CATEGORY_SELECTED_ALTERNATIVE} to find activities that have
250 * said they would like to be included as optional action. You can, however,
251 * use any Intent you want.
254 * See {@link android.content.pm.PackageManager#queryIntentActivityOptions}
255 * for more * details on the <var>caller</var>, <var>specifics</var>, and
256 * <var>intent</var> arguments. The list returned by that function is used
257 * to populate the resulting menu items.
260 * All of the menu items of possible options for the intent will be added
261 * with the given group and id. You can use the group to control ordering of
262 * the items in relation to other items in the menu. Normally this function
263 * will automatically remove any existing items in the menu in the same
264 * group and place a divider above and below the added items; this behavior
265 * can be modified with the <var>flags</var> parameter. For each of the
266 * generated items {@link MenuItem#setIntent} is called to associate the
267 * appropriate Intent with the item; this means the activity will
268 * automatically be started for you without having to do anything else.
270 * @param groupId The group identifier that the items should be part of.
271 * This can also be used to define groups of items for batch state
272 * changes. Normally use {@link #NONE} if the items should not be in
274 * @param itemId Unique item ID. Use {@link #NONE} if you do not need a
276 * @param order The order for the items. Use {@link #NONE} if you do not
277 * care about the order. See {@link MenuItem#getOrder()}.
278 * @param caller The current activity component name as defined by
279 * queryIntentActivityOptions().
280 * @param specifics Specific items to place first as defined by
281 * queryIntentActivityOptions().
282 * @param intent Intent describing the kinds of items to populate in the
283 * list as defined by queryIntentActivityOptions().
284 * @param flags Additional options controlling how the items are added.
285 * @param outSpecificItems Optional array in which to place the menu items
286 * that were generated for each of the <var>specifics</var> that were
287 * requested. Entries may be null if no activity was found for that
289 * @return The number of menu items that were added.
291 * @see #FLAG_APPEND_TO_GROUP
292 * @see MenuItem#setIntent
293 * @see android.content.pm.PackageManager#queryIntentActivityOptions
295 public int addIntentOptions(int groupId
, int itemId
, int order
,
296 ComponentName caller
, Intent
[] specifics
,
297 Intent intent
, int flags
, MenuItem
[] outSpecificItems
);
300 * Remove the item with the given identifier.
302 * @param id The item to be removed. If there is no item with this
303 * identifier, nothing happens.
305 public void removeItem(int id
);
308 * Remove all items in the given group.
310 * @param groupId The group to be removed. If there are no items in this
311 * group, nothing happens.
313 public void removeGroup(int groupId
);
316 * Remove all existing items from the menu, leaving it empty as if it had
322 * Control whether a particular group of items can show a check mark. This
323 * is similar to calling {@link MenuItem#setCheckable} on all of the menu items
324 * with the given group identifier, but in addition you can control whether
325 * this group contains a mutually-exclusive set items. This should be called
326 * after the items of the group have been added to the menu.
328 * @param group The group of items to operate on.
329 * @param checkable Set to true to allow a check mark, false to
330 * disallow. The default is false.
331 * @param exclusive If set to true, only one item in this group can be
332 * checked at a time; checking an item will automatically
333 * uncheck all others in the group. If set to false, each
334 * item can be checked independently of the others.
336 * @see MenuItem#setCheckable
337 * @see MenuItem#setChecked
339 public void setGroupCheckable(int group
, boolean checkable
, boolean exclusive
);
342 * Show or hide all menu items that are in the given group.
344 * @param group The group of items to operate on.
345 * @param visible If true the items are visible, else they are hidden.
347 * @see MenuItem#setVisible
349 public void setGroupVisible(int group
, boolean visible
);
352 * Enable or disable all menu items that are in the given group.
354 * @param group The group of items to operate on.
355 * @param enabled If true the items will be enabled, else they will be disabled.
357 * @see MenuItem#setEnabled
359 public void setGroupEnabled(int group
, boolean enabled
);
362 * Return whether the menu currently has item items that are visible.
364 * @return True if there is one or more item visible,
367 public boolean hasVisibleItems();
370 * Return the menu item with a particular identifier.
372 * @param id The identifier to find.
374 * @return The menu item object, or null if there is no item with
377 public MenuItem
findItem(int id
);
380 * Get the number of items in the menu. Note that this will change any
381 * times items are added or removed from the menu.
383 * @return The item count.
388 * Gets the menu item at the given index.
390 * @param index The index of the menu item to return.
391 * @return The menu item.
392 * @exception IndexOutOfBoundsException
393 * when {@code index < 0 || >= size()}
395 public MenuItem
getItem(int index
);
398 * Closes the menu, if open.
403 * Execute the menu item action associated with the given shortcut
406 * @param keyCode The keycode of the shortcut key.
407 * @param event Key event message.
408 * @param flags Additional option flags or 0.
410 * @return If the given shortcut exists and is shown, returns
411 * true; else returns false.
413 * @see #FLAG_PERFORM_NO_CLOSE
415 public boolean performShortcut(int keyCode
, KeyEvent event
, int flags
);
418 * Is a keypress one of the defined shortcut keys for this window.
419 * @param keyCode the key code from {@link KeyEvent} to check.
420 * @param event the {@link KeyEvent} to use to help check.
422 boolean isShortcutKey(int keyCode
, KeyEvent event
);
425 * Execute the menu item action associated with the given menu identifier.
427 * @param id Identifier associated with the menu item.
428 * @param flags Additional option flags or 0.
430 * @return If the given identifier exists and is shown, returns
431 * true; else returns false.
433 * @see #FLAG_PERFORM_NO_CLOSE
435 public boolean performIdentifierAction(int id
, int flags
);
439 * Control whether the menu should be running in qwerty mode (alphabetic
440 * shortcuts) or 12-key mode (numeric shortcuts).
442 * @param isQwerty If true the menu will use alphabetic shortcuts; else it
443 * will use numeric shortcuts.
445 public void setQwertyMode(boolean isQwerty
);