Interface MenuItem
- All Known Implementing Classes:
MenuItemImpl
An Item is returned by calling one of the Menu.add(java.lang.CharSequence)
methods.
For a feature set of specific menu types, see Menu
.
Developer Guides
For information about creating menus, read the Menus developer guide.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Interface definition for a callback to be invoked when a menu item marked withSHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
is expanded or collapsed.static interface
Interface definition for a callback to be invoked when a menu item is clicked. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Always show this item as a button in an Action Bar.static final int
This item's action view collapses to a normal menu item.static final int
Show this item as a button in an Action Bar if the system decides there is room for it.static final int
Never show this item as a button in an Action Bar.static final int
When this item is in the action bar, always show it with a text label even if it also has an icon specified. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Collapse the action view associated with this menu item.boolean
Expand the action view associated with this menu item.Gets theActionProvider
.Returns the currently set action view for this menu item.default int
Return the modifier for this menu item's alphabetic shortcut.char
Return the char for this menu item's alphabetic shortcut.default CharSequence
Retrieve the content description associated with this menu item.int
Return the group identifier that this menu item is part of.getIcon()
Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before).default ColorStateList
int
Return the identifier for this menu item.Gets the extra information linked to this menu item.default int
Return the modifiers for this menu item's numeric (12-key) shortcut.char
Return the char for this menu item's numeric (12-key) shortcut.int
getOrder()
Return the category and order within the category of this item.Get the sub-menu to be invoked when this item is selected, if it has one.getTitle()
Retrieve the current title of the item.Retrieve the current condensed title of the item.default CharSequence
Retrieve the tooltip text associated with this menu item.boolean
Check whether this item has an associated sub-menu.boolean
Returns true if this menu item's action view has been expanded.boolean
Return whether the item can currently display a check mark.boolean
Return whether the item is currently displaying a check mark.boolean
Return the enabled state of the menu item.boolean
Return the visibility of the menu item.default boolean
Returns true ifsetShowAsAction(int)
was set toSHOW_AS_ACTION_ALWAYS
.default boolean
Returns true ifsetShowAsAction(int)
was set toSHOW_AS_ACTION_NEVER
.setActionProvider
(ActionProvider actionProvider) Sets theActionProvider
responsible for creating an action view if the item is placed on the action bar.setActionView
(View view) Set an action view for this menu item.setAlphabeticShortcut
(char alphaChar) Change the alphabetic shortcut associated with this item.default MenuItem
setAlphabeticShortcut
(char alphaChar, int alphaModifiers) Change the alphabetic shortcut associated with this item.setCheckable
(boolean checkable) Control whether this item can display a check mark.setChecked
(boolean checked) Control whether this item is shown with a check mark.default MenuItem
setContentDescription
(CharSequence contentDescription) Change the content description associated with this menu item.setEnabled
(boolean enabled) Sets whether the menu item is enabled.Change the icon associated with this item.default MenuItem
Applies a tint to this item's icon.setNumericShortcut
(char numericChar) Change the numeric shortcut associated with this item.default MenuItem
setNumericShortcut
(char numericChar, int numericModifiers) Change the numeric shortcut and modifiers associated with this item.Set anMenuItem.OnActionExpandListener
on this menu item to be notified when the associated action view is expanded or collapsed.setOnMenuItemClickListener
(MenuItem.OnMenuItemClickListener menuItemClickListener) Set a custom listener for invocation of this menu item.setShortcut
(char numericChar, char alphaChar) Change both the numeric and alphabetic shortcut associated with this item.default MenuItem
setShortcut
(char numericChar, char alphaChar, int numericModifiers, int alphaModifiers) Change both the numeric and alphabetic shortcut associated with this item.void
setShowAsAction
(int actionEnum) Sets how this item should display in the presence of an Action Bar.setShowAsActionFlags
(int actionEnum) Sets how this item should display in the presence of an Action Bar.setTitle
(CharSequence title) Change the title associated with this item.setTitleCondensed
(CharSequence title) Change the condensed title associated with this item.default MenuItem
setTooltipText
(CharSequence tooltipText) Change the tooltip text associated with this menu item.setVisible
(boolean visible) Sets the visibility of the menu item.
-
Field Details
-
SHOW_AS_ACTION_NEVER
static final int SHOW_AS_ACTION_NEVERNever show this item as a button in an Action Bar.- See Also:
-
SHOW_AS_ACTION_IF_ROOM
static final int SHOW_AS_ACTION_IF_ROOMShow this item as a button in an Action Bar if the system decides there is room for it.- See Also:
-
SHOW_AS_ACTION_ALWAYS
static final int SHOW_AS_ACTION_ALWAYSAlways show this item as a button in an Action Bar. Use sparingly! If too many items are set to always show in the Action Bar it can crowd the Action Bar and degrade the user experience on devices with smaller screens. A good rule of thumb is to have no more than 2 items set to always show at a time.- See Also:
-
SHOW_AS_ACTION_WITH_TEXT
static final int SHOW_AS_ACTION_WITH_TEXTWhen this item is in the action bar, always show it with a text label even if it also has an icon specified.- See Also:
-
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEWThis item's action view collapses to a normal menu item. When expanded, the action view temporarily takes over a larger segment of its container.- See Also:
-
-
Method Details
-
getItemId
int getItemId()Return the identifier for this menu item. The identifier can not be changed after the menu is created.- Returns:
- The menu item's identifier.
-
getGroupId
int getGroupId()Return the group identifier that this menu item is part of. The group identifier can not be changed after the menu is created.- Returns:
- The menu item's group identifier.
-
getOrder
int getOrder()Return the category and order within the category of this item. This item will be shown before all items (within its category) that have order greater than this value.An order integer contains the item's category (the upper bits of the integer; set by or/add the category with the order within the category) and the ordering of the item within that category (the lower bits). Example categories are
Menu.CATEGORY_SYSTEM
,Menu.CATEGORY_SECONDARY
,Menu.CATEGORY_ALTERNATIVE
,Menu.CATEGORY_CONTAINER
. SeeMenu
for a full list.- Returns:
- The order of this item.
-
setTitle
Change the title associated with this item.- Parameters:
title
- The new text to be displayed.- Returns:
- This Item so additional setters can be called.
-
getTitle
CharSequence getTitle()Retrieve the current title of the item.- Returns:
- The title.
-
setTitleCondensed
Change the condensed title associated with this item. The condensed title is used in situations where the normal title may be too long to be displayed.- Parameters:
title
- The new text to be displayed as the condensed title.- Returns:
- This Item so additional setters can be called.
-
getTitleCondensed
CharSequence getTitleCondensed()Retrieve the current condensed title of the item. If a condensed title was never set, it will return the normal title.- Returns:
- The condensed title, if it exists. The normal title otherwise.
-
setIcon
Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. SeeMenu
for the menu types that support icons.- Parameters:
icon
- The new icon (as a Drawable) to be displayed.- Returns:
- This Item so additional setters can be called.
-
getIcon
Drawable getIcon()Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before). Note that if you callsetIconTintList(ColorStateList)
on this item, and you use a custom menu presenter in your application, you have to apply the tinting explicitly on theDrawable
returned by this method.- Returns:
- The icon as a Drawable.
-
setIconTintList
Applies a tint to this item's icon.Subsequent calls to
setIcon(Drawable)
will automatically mutate the icon and apply the specified tint and tint mode usingDrawable.setTintList(ColorStateList)
.- Parameters:
tint
- the tint to apply, may benull
to clear tint- See Also:
-
getIconTintList
- Returns:
- the tint applied to this item's icon
- See Also:
-
setShortcut
Change both the numeric and alphabetic shortcut associated with this item. Note that the shortcut will be triggered when the key that generates the given character is pressed along with the corresponding modifier key. The default modifier isKeyEvent.META_CTRL_ON
in case nothing is specified. Also note that case is not significant and that alphabetic shortcut characters will be handled in lower case.See
Menu
for the menu types that support shortcuts.- Parameters:
numericChar
- The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard.alphaChar
- The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.- Returns:
- This Item so additional setters can be called.
-
setShortcut
default MenuItem setShortcut(char numericChar, char alphaChar, int numericModifiers, int alphaModifiers) Change both the numeric and alphabetic shortcut associated with this item. Note that the shortcut will be triggered when the key that generates the given character is pressed along with the corresponding modifier key. Also note that case is not significant and that alphabetic shortcut characters will be handled in lower case.See
Menu
for the menu types that support shortcuts.- Parameters:
numericChar
- The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard.alphaChar
- The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.numericModifiers
- The numeric modifier associated with the shortcut. It should be a combination ofKeyEvent.META_CTRL_ON
,KeyEvent.META_ALT_ON
,KeyEvent.META_SHIFT_ON
.alphaModifiers
- The alphabetic modifier associated with the shortcut. It should be a combination ofKeyEvent.META_CTRL_ON
,KeyEvent.META_ALT_ON
,KeyEvent.META_SHIFT_ON
.- Returns:
- This Item so additional setters can be called.
-
setNumericShortcut
Change the numeric shortcut associated with this item.See
Menu
for the menu types that support shortcuts.- Parameters:
numericChar
- The numeric shortcut key. This is the shortcut when using a 12-key (numeric) keyboard.- Returns:
- This Item so additional setters can be called.
-
setNumericShortcut
Change the numeric shortcut and modifiers associated with this item.See
Menu
for the menu types that support shortcuts.- Parameters:
numericChar
- The numeric shortcut key. This is the shortcut when using a 12-key (numeric) keyboard.numericModifiers
- The modifier associated with the shortcut. It should be a combination ofKeyEvent.META_CTRL_ON
,KeyEvent.META_ALT_ON
,KeyEvent.META_SHIFT_ON
.- Returns:
- This Item so additional setters can be called.
-
getNumericShortcut
char getNumericShortcut()Return the char for this menu item's numeric (12-key) shortcut.- Returns:
- Numeric character to use as a shortcut.
-
getNumericModifiers
default int getNumericModifiers()Return the modifiers for this menu item's numeric (12-key) shortcut. The modifier is a combination ofKeyEvent.META_CTRL_ON
,KeyEvent.META_ALT_ON
,KeyEvent.META_SHIFT_ON
.- Returns:
- Modifier associated with the numeric shortcut.
-
setAlphabeticShortcut
Change the alphabetic shortcut associated with this item. The shortcut will be triggered when the key that generates the given character is pressed along with the corresponding modifier key. The default modifier isKeyEvent.META_CTRL_ON
in case nothing is specified. Case is not significant and shortcut characters will be displayed in lower case. Note that menu items with the characters '\b' or '\n' as shortcuts will get triggered by the Delete key or Carriage Return key, respectively.See
Menu
for the menu types that support shortcuts.- Parameters:
alphaChar
- The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.- Returns:
- This Item so additional setters can be called.
-
setAlphabeticShortcut
Change the alphabetic shortcut associated with this item. The shortcut will be triggered when the key that generates the given character is pressed along with the modifier keys. Case is not significant and shortcut characters will be displayed in lower case. Note that menu items with the characters '\b' or '\n' as shortcuts will get triggered by the Delete key or Carriage Return key, respectively.See
Menu
for the menu types that support shortcuts.- Parameters:
alphaChar
- The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.alphaModifiers
- The modifier associated with the shortcut. It should be a combination ofKeyEvent.META_CTRL_ON
,KeyEvent.META_ALT_ON
,KeyEvent.META_SHIFT_ON
.- Returns:
- This Item so additional setters can be called.
-
getAlphabeticShortcut
char getAlphabeticShortcut()Return the char for this menu item's alphabetic shortcut.- Returns:
- Alphabetic character to use as a shortcut.
-
getAlphabeticModifiers
default int getAlphabeticModifiers()Return the modifier for this menu item's alphabetic shortcut. The modifier is a combination ofKeyEvent.META_CTRL_ON
,KeyEvent.META_ALT_ON
,KeyEvent.META_SHIFT_ON
.- Returns:
- Modifier associated with the keyboard shortcut.
-
setCheckable
Control whether this item can display a check mark. Setting this does not actually display a check mark (seesetChecked(boolean)
for that); rather, it ensures there is room in the item in which to display a check mark.See
Menu
for the menu types that support check marks.- Parameters:
checkable
- Set to true to allow a check mark, false to disallow. The default is false.- Returns:
- This Item so additional setters can be called.
- See Also:
-
isCheckable
boolean isCheckable()Return whether the item can currently display a check mark.- Returns:
- If a check mark can be displayed, returns true.
- See Also:
-
setChecked
Control whether this item is shown with a check mark. Note that you must first have enabled checking withsetCheckable(boolean)
or else the check mark will not appear. If this item is a member of a group that contains mutually-exclusive items (set viaMenu.setGroupCheckable(int, boolean, boolean)
), the other items in the group will be unchecked.See
Menu
for the menu types that support check marks.- Parameters:
checked
- Set to true to display a check mark, false to hide it. The default value is false.- Returns:
- This Item so additional setters can be called.
- See Also:
-
isChecked
boolean isChecked()Return whether the item is currently displaying a check mark.- Returns:
- If a check mark is displayed, returns true.
- See Also:
-
setVisible
Sets the visibility of the menu item. Even if a menu item is not visible, it may still be invoked via its shortcut (to completely disable an item, set it to invisible anddisabled
).- Parameters:
visible
- If true then the item will be visible; if false it is hidden.- Returns:
- This Item so additional setters can be called.
-
isVisible
boolean isVisible()Return the visibility of the menu item.- Returns:
- If true the item is visible; else it is hidden.
-
setEnabled
Sets whether the menu item is enabled. Disabling a menu item will not allow it to be invoked via its shortcut. The menu item will still be visible.- Parameters:
enabled
- If true then the item will be invokable; if false it is won't be invokable.- Returns:
- This Item so additional setters can be called.
-
isEnabled
boolean isEnabled()Return the enabled state of the menu item.- Returns:
- If true the item is enabled and hence invokable; else it is not.
-
hasSubMenu
boolean hasSubMenu()Check whether this item has an associated sub-menu. I.e. it is a sub-menu of another menu.- Returns:
- If true this item has a menu; else it is a normal item.
-
getSubMenu
SubMenu getSubMenu()Get the sub-menu to be invoked when this item is selected, if it has one. SeehasSubMenu()
.- Returns:
- The associated menu if there is one, else null
-
setOnMenuItemClickListener
Set a custom listener for invocation of this menu item.- Parameters:
menuItemClickListener
- The object to receive invokations.- Returns:
- This Item so additional setters can be called.
-
getMenuInfo
Gets the extra information linked to this menu item. This extra information is set by the View that added this menu item to the menu.- Returns:
- The extra information linked to the View that added this menu item to the menu. This can be null.
- See Also:
-
setShowAsAction
void setShowAsAction(int actionEnum) Sets how this item should display in the presence of an Action Bar. The parameter actionEnum is a flag set. One ofSHOW_AS_ACTION_ALWAYS
,SHOW_AS_ACTION_IF_ROOM
, orSHOW_AS_ACTION_NEVER
should be used, and you may optionally OR the value withSHOW_AS_ACTION_WITH_TEXT
. SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action, it should be shown with a text label.- Parameters:
actionEnum
- How the item should display. One ofSHOW_AS_ACTION_ALWAYS
,SHOW_AS_ACTION_IF_ROOM
, orSHOW_AS_ACTION_NEVER
. SHOW_AS_ACTION_NEVER is the default.- See Also:
-
setShowAsActionFlags
Sets how this item should display in the presence of an Action Bar. The parameter actionEnum is a flag set. One ofSHOW_AS_ACTION_ALWAYS
,SHOW_AS_ACTION_IF_ROOM
, orSHOW_AS_ACTION_NEVER
should be used, and you may optionally OR the value withSHOW_AS_ACTION_WITH_TEXT
. SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action, it should be shown with a text label.Note: This method differs from
setShowAsAction(int)
only in that it returns the current MenuItem instance for call chaining.- Parameters:
actionEnum
- How the item should display. One ofSHOW_AS_ACTION_ALWAYS
,SHOW_AS_ACTION_IF_ROOM
, orSHOW_AS_ACTION_NEVER
. SHOW_AS_ACTION_NEVER is the default.- Returns:
- This MenuItem instance for call chaining.
- See Also:
-
setActionView
Set an action view for this menu item. An action view will be displayed in place of an automatically generated menu item element in the UI when this item is shown as an action within a parent.Note: Setting an action view overrides the action provider set via
setActionProvider(ActionProvider)
.- Parameters:
view
- View to use for presenting this item to the user.- Returns:
- This Item so additional setters can be called.
- See Also:
-
getActionView
View getActionView()Returns the currently set action view for this menu item.- Returns:
- This item's action view
- See Also:
-
setActionProvider
Sets theActionProvider
responsible for creating an action view if the item is placed on the action bar. The provider also provides a default action invoked if the item is placed in the overflow menu.Note: Setting an action provider overrides the action view set via
setActionView(View)
.- Parameters:
actionProvider
- The action provider.- Returns:
- This Item so additional setters can be called.
- See Also:
-
getActionProvider
ActionProvider getActionProvider()Gets theActionProvider
.- Returns:
- The action provider.
- See Also:
-
expandActionView
boolean expandActionView()Expand the action view associated with this menu item. The menu item must have an action view set, as well as the showAsAction flagSHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
. If a listener has been set usingsetOnActionExpandListener(OnActionExpandListener)
it will have itsMenuItem.OnActionExpandListener.onMenuItemActionExpand(MenuItem)
method invoked. The listener may return false from this method to prevent expanding the action view.- Returns:
- true if the action view was expanded, false otherwise.
-
collapseActionView
boolean collapseActionView()Collapse the action view associated with this menu item. The menu item must have an action view set, as well as the showAsAction flagSHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
. If a listener has been set usingsetOnActionExpandListener(OnActionExpandListener)
it will have itsMenuItem.OnActionExpandListener.onMenuItemActionCollapse(MenuItem)
method invoked. The listener may return false from this method to prevent collapsing the action view.- Returns:
- true if the action view was collapsed, false otherwise.
-
isActionViewExpanded
boolean isActionViewExpanded()Returns true if this menu item's action view has been expanded.- Returns:
- true if the item's action view is expanded, false otherwise.
- See Also:
-
setOnActionExpandListener
Set anMenuItem.OnActionExpandListener
on this menu item to be notified when the associated action view is expanded or collapsed. The menu item must be configured to expand or collapse its action view using the flagSHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
.- Parameters:
listener
- Listener that will respond to expand/collapse events- Returns:
- This menu item instance for call chaining
-
setContentDescription
Change the content description associated with this menu item.- Parameters:
contentDescription
- The new content description.
-
getContentDescription
Retrieve the content description associated with this menu item.- Returns:
- The content description.
-
setTooltipText
Change the tooltip text associated with this menu item.- Parameters:
tooltipText
- The new tooltip text.
-
getTooltipText
Retrieve the tooltip text associated with this menu item.- Returns:
- The tooltip text.
-
requiresActionButton
@Internal default boolean requiresActionButton() -
requiresOverflow
@Internal default boolean requiresOverflow()
-