Class MenuBuilder
- All Implemented Interfaces:
Menu
- Direct Known Subclasses:
ContextMenuBuilder,SubMenuBuilder
Menu interface for creating a
standard menu UI.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCalled by menu to notify of close and selection changes.static interfaceCalled by menu items to execute their associated action -
Field Summary
Fields inherited from interface icyllis.modernui.view.Menu
CATEGORY_ALTERNATIVE, CATEGORY_CONTAINER, CATEGORY_MASK, CATEGORY_SECONDARY, CATEGORY_SHIFT, CATEGORY_SYSTEM, FIRST, FLAG_ALWAYS_PERFORM_CLOSE, FLAG_PERFORM_NO_CLOSE, NONE, SUPPORTED_MODIFIERS_MASK, USER_MASK, USER_SHIFT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(int group, int id, int categoryOrder, CharSequence title) Add a new item to the menu.add(CharSequence title) Add a new item to the menu.voidaddMenuPresenter(MenuPresenter presenter) Add a presenter to this menu.voidaddMenuPresenter(MenuPresenter presenter, Context menuContext) Add a presenter to this menu that uses an alternate context for inflating menu items.addSubMenu(int group, int id, int categoryOrder, CharSequence title) Add a new sub-menu to the menu.addSubMenu(CharSequence title) Add a new sub-menu to the menu.voidDispatch a mode change event to this menu's callback.voidclear()Remove all existing items from the menu, leaving it empty as if it had just been created.voidclearAll()voidvoidclose()Closes the menu, if open.final voidclose(boolean closeAllMenus) Closes the menu.booleanbooleanintfindGroupIndex(int group) intfindGroupIndex(int group, int start) findItem(int id) Return the menu item with a particular identifier.intfindItemIndex(int id) voidThis method determines which menu items get to be 'action items' that will appear in an action bar and which items should be 'overflow items' in a secondary menu.getItem(int index) Gets the menu item at the given index.Gets the root menu (if this is a submenu, find its root menu).booleanReturn whether the menu currently has item items that are visible.booleanbooleanisShortcutKey(int keyCode, KeyEvent event) Is a keypress one of the defined shortcut keys for this window.booleanvoidonItemsChanged(boolean structureChanged) Called when an item is added or removed.booleanperformIdentifierAction(int id, int flags) Execute the menu item action associated with the given menu identifier.booleanperformItemAction(MenuItem item, int flags) booleanperformItemAction(MenuItem item, MenuPresenter preferredPresenter, int flags) booleanperformShortcut(int keyCode, KeyEvent event, int flags) Execute the menu item action associated with the given shortcut character.voidremoveGroup(int group) Remove all items in the given group.voidremoveItem(int id) Remove the item with the given identifier.voidremoveItemAt(int index) voidremoveMenuPresenter(MenuPresenter presenter) Remove a presenter from this menu.voidvoidSets the current menu info that is set on all items added to this menu (until this is called again with different menu info, in which case that one will be added to all subsequent item additions).setDefaultShowAsAction(int defaultShowAsAction) voidsetGroupCheckable(int group, boolean checkable, boolean exclusive) Control whether a particular group of items can show a check mark.voidsetGroupDividerEnabled(boolean groupDividerEnabled) Enable or disable the group dividers.voidsetGroupEnabled(int group, boolean enabled) Enable or disable all menu items that are in the given group.voidsetGroupVisible(int group, boolean visible) Show or hide all menu items that are in the given group.protected MenuBuildersetHeaderIconInt(Drawable icon) Sets the header's icon.protected MenuBuildersetHeaderTitleInt(CharSequence title) Sets the header's title.protected MenuBuildersetHeaderViewInt(View view) Sets the header's view.voidsetOptionalIconsVisible(boolean visible) Sets the optional icon visible.voidsetQwertyMode(boolean isQwerty) Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts).voidsetShortcutsVisible(boolean shortcutsVisible) Sets whether the shortcuts should be visible on menus.intsize()Get the number of items in the menu.voidvoidStop dispatching item changed events to presenters untilstartDispatchingItemsChanged()is called.
-
Constructor Details
-
MenuBuilder
-
-
Method Details
-
setDefaultShowAsAction
-
add
Description copied from interface:MenuAdd a new item to the menu. This item displays the given title for its label. -
add
Description copied from interface:MenuAdd a new item to the menu. This item displays the given title for its label.- Specified by:
addin interfaceMenu- Parameters:
group- The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally useMenu.NONEif an item should not be in a group.id- Unique item ID. UseMenu.NONEif you do not need a unique ID.categoryOrder- The order for the item. UseMenu.NONEif you do not care about the order. SeeMenuItem.getOrder().title- The text to display for the item.- Returns:
- The newly added menu item.
-
addSubMenu
Description copied from interface:MenuAdd a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, useSubMenu.getItem().- Specified by:
addSubMenuin interfaceMenu- Parameters:
title- The text to display for the item.- Returns:
- The newly added sub-menu
-
addSubMenu
@NonNull public SubMenu addSubMenu(int group, int id, int categoryOrder, @Nullable CharSequence title) Description copied from interface:MenuAdd a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, useSubMenu.getItem().Note that you can only have one level of sub-menus, i.e. you cannot add a subMenu to a subMenu: An
UnsupportedOperationExceptionwill be thrown if you try.- Specified by:
addSubMenuin interfaceMenu- Parameters:
group- The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally useMenu.NONEif an item should not be in a group.id- Unique item ID. UseMenu.NONEif you do not need a unique ID.categoryOrder- The order for the item. UseMenu.NONEif you do not care about the order. SeeMenuItem.getOrder().title- The text to display for the item.- Returns:
- The newly added sub-menu
-
setGroupDividerEnabled
public void setGroupDividerEnabled(boolean groupDividerEnabled) Description copied from interface:MenuEnable or disable the group dividers.- Specified by:
setGroupDividerEnabledin interfaceMenu
-
isGroupDividerEnabled
public boolean isGroupDividerEnabled() -
removeItem
public void removeItem(int id) Description copied from interface:MenuRemove the item with the given identifier.- Specified by:
removeItemin interfaceMenu- Parameters:
id- The item to be removed. If there is no item with this identifier, nothing happens.
-
removeGroup
public void removeGroup(int group) Description copied from interface:MenuRemove all items in the given group.- Specified by:
removeGroupin interfaceMenu- Parameters:
group- The group to be removed. If there are no items in this group, nothing happens.
-
removeItemAt
public void removeItemAt(int index) -
clearAll
public void clearAll() -
clear
public void clear()Description copied from interface:MenuRemove all existing items from the menu, leaving it empty as if it had just been created. -
setGroupCheckable
public void setGroupCheckable(int group, boolean checkable, boolean exclusive) Description copied from interface:MenuControl whether a particular group of items can show a check mark. This is similar to callingMenuItem.setCheckable(boolean)on all the menu items with the given group identifier, but in addition you can control whether this group contains a mutually-exclusive set items. This should be called after the items of the group have been added to the menu.- Specified by:
setGroupCheckablein interfaceMenu- Parameters:
group- The group of items to operate on.checkable- Set to true to allow a check mark, false to disallow. The default is false.exclusive- If set to true, only one item in this group can be checked at a time; checking an item will automatically uncheck all others in the group. If set to false, each item can be checked independently of the others.- See Also:
-
setGroupVisible
public void setGroupVisible(int group, boolean visible) Description copied from interface:MenuShow or hide all menu items that are in the given group.- Specified by:
setGroupVisiblein interfaceMenu- Parameters:
group- The group of items to operate on.visible- If true the items are visible, else they are hidden.- See Also:
-
setGroupEnabled
public void setGroupEnabled(int group, boolean enabled) Description copied from interface:MenuEnable or disable all menu items that are in the given group.- Specified by:
setGroupEnabledin interfaceMenu- Parameters:
group- The group of items to operate on.enabled- If true the items will be enabled, else they will be disabled.- See Also:
-
hasVisibleItems
public boolean hasVisibleItems()Description copied from interface:MenuReturn whether the menu currently has item items that are visible.- Specified by:
hasVisibleItemsin interfaceMenu- Returns:
- True if there is one or more item visible, else false.
-
findItem
Description copied from interface:MenuReturn the menu item with a particular identifier. -
findItemIndex
public int findItemIndex(int id) -
findGroupIndex
public int findGroupIndex(int group) -
findGroupIndex
public int findGroupIndex(int group, int start) -
size
public int size()Description copied from interface:MenuGet the number of items in the menu. Note that this will change any times items are added or removed from the menu. -
getItem
Gets the menu item at the given index. -
isShortcutKey
Description copied from interface:MenuIs a keypress one of the defined shortcut keys for this window.- Specified by:
isShortcutKeyin interfaceMenu- Parameters:
keyCode- the key code fromKeyEventto check.event- theKeyEventto use to help check.
-
setQwertyMode
public void setQwertyMode(boolean isQwerty) Description copied from interface:MenuControl whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts).- Specified by:
setQwertyModein interfaceMenu- Parameters:
isQwerty- If true the menu will use alphabetic shortcuts; else it will use numeric shortcuts.
-
setShortcutsVisible
public void setShortcutsVisible(boolean shortcutsVisible) Sets whether the shortcuts should be visible on menus. Devices without hardware key input will never make shortcuts visible even if this method is passed 'true'.- Parameters:
shortcutsVisible- Whether shortcuts should be visible (if true and a menu item does not have a shortcut defined, that item will still NOT show a shortcut)
-
isShortcutsVisible
public boolean isShortcutsVisible()- Returns:
- Whether shortcuts should be visible on menus.
-
getContext
-
changeMenuMode
public void changeMenuMode()Dispatch a mode change event to this menu's callback. -
performShortcut
Description copied from interface:MenuExecute the menu item action associated with the given shortcut character.- Specified by:
performShortcutin interfaceMenu- Parameters:
keyCode- The keycode of the shortcut key.event- Key event message.flags- Additional option flags or 0.- Returns:
- If the given shortcut exists and is shown, returns true; else returns false.
- See Also:
-
performIdentifierAction
public boolean performIdentifierAction(int id, int flags) Description copied from interface:MenuExecute the menu item action associated with the given menu identifier.- Specified by:
performIdentifierActionin interfaceMenu- Parameters:
id- Identifier associated with the menu item.flags- Additional option flags or 0.- Returns:
- If the given identifier exists and is shown, returns true; else returns false.
- See Also:
-
performItemAction
-
close
public final void close(boolean closeAllMenus) Closes the menu.- Parameters:
closeAllMenus-trueif all displayed menus and submenus should be completely closed (as when a menu item is selected) orfalseif only this menu should be closed
-
close
public void close()Closes the menu, if open. -
onItemsChanged
public void onItemsChanged(boolean structureChanged) Called when an item is added or removed.- Parameters:
structureChanged- true if the menu structure changed, false if only item properties changed. (Visibility is a structural property since it affects layout.)
-
stopDispatchingItemsChanged
public void stopDispatchingItemsChanged()Stop dispatching item changed events to presenters untilstartDispatchingItemsChanged()is called. Useful when many menu operations are going to be performed as a batch. -
startDispatchingItemsChanged
public void startDispatchingItemsChanged() -
getVisibleItems
-
flagActionItems
public void flagActionItems()This method determines which menu items get to be 'action items' that will appear in an action bar and which items should be 'overflow items' in a secondary menu. The rules are as follows:Items are considered for inclusion in the order specified within the menu. There is a limit of mMaxActionItems as a total count, optionally including the overflow menu button itself. This is a soft limit; if an item shares a group ID with an item previously included as an action item, the new item will stay with its group and become an action item itself even if it breaks the max item count limit. This is done to limit the conceptual complexity of the items presented within an action bar. Only a few unrelated concepts should be presented to the user in this space, and groups are treated as a single concept.
There is also a hard limit of consumed measurable space: mActionWidthLimit. This limit may be broken by a single item that exceeds the remaining space, but no further items may be added. If an item that is part of a group cannot fit within the remaining measured width, the entire group will be demoted to overflow. This is done to ensure room for navigation and other affordances in the action bar as well as reduce general UI clutter.
The space freed by demoting a full group cannot be consumed by future menu items. Once items begin to overflow, all future items become overflow items as well. This is to avoid inadvertent reordering that may break the app's intended design.
-
getActionItems
-
getNonActionItems
-
clearHeader
public void clearHeader() -
setHeaderTitleInt
Sets the header's title. This replaces the header view. Called by the builder-style methods of subclasses.- Parameters:
title- The new title.- Returns:
- This MenuBuilder so additional setters can be called.
-
setHeaderIconInt
Sets the header's icon. This replaces the header view. Called by the builder-style methods of subclasses.- Parameters:
icon- The new icon.- Returns:
- This MenuBuilder so additional setters can be called.
-
setHeaderViewInt
Sets the header's view. This replaces the title and icon. Called by the builder-style methods of subclasses.- Parameters:
view- The new view.- Returns:
- This MenuBuilder so additional setters can be called.
-
getHeaderTitle
-
getHeaderIcon
-
getHeaderView
-
getRootMenu
Gets the root menu (if this is a submenu, find its root menu).- Returns:
- The root menu.
-
setCurrentMenuInfo
Sets the current menu info that is set on all items added to this menu (until this is called again with different menu info, in which case that one will be added to all subsequent item additions).- Parameters:
menuInfo- The extra menu information to add.
-
setOptionalIconsVisible
public void setOptionalIconsVisible(boolean visible) Description copied from interface:MenuSets the optional icon visible.- Specified by:
setOptionalIconsVisiblein interfaceMenu- Parameters:
visible- true for visible, false for hidden.
-
getExpandedItem
-