Skip to content

Most visited

Recently visited

navigation
added in API level 1

MenuItem

public interface MenuItem

android.view.MenuItem


Interface for direct access to a previously created menu item.

An Item is returned by calling one of the add(int) methods.

For a feature set of specific menu types, see Menu.

Developer Guides

For information about creating menus, read the Menus developer guide.

Summary

Nested classes

interface MenuItem.OnActionExpandListener

Interface definition for a callback to be invoked when a menu item marked with SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW is expanded or collapsed. 

interface MenuItem.OnMenuItemClickListener

Interface definition for a callback to be invoked when a menu item is clicked. 

XML attributes

android:iconTint Tint to apply to the icon. 
android:iconTintMode Blending mode used to apply the icon tint. 

Constants

int SHOW_AS_ACTION_ALWAYS

Always show this item as a button in an Action Bar.

int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

This item's action view collapses to a normal menu item.

int SHOW_AS_ACTION_IF_ROOM

Show this item as a button in an Action Bar if the system decides there is room for it.

int SHOW_AS_ACTION_NEVER

Never show this item as a button in an Action Bar.

int SHOW_AS_ACTION_WITH_TEXT

When this item is in the action bar, always show it with a text label even if it also has an icon specified.

Public methods

abstract boolean collapseActionView()

Collapse the action view associated with this menu item.

abstract boolean expandActionView()

Expand the action view associated with this menu item.

abstract ActionProvider getActionProvider()

Gets the ActionProvider.

abstract View getActionView()

Returns the currently set action view for this menu item.

default int getAlphabeticModifiers()

Return the modifier for this menu item's alphabetic shortcut.

abstract char getAlphabeticShortcut()

Return the char for this menu item's alphabetic shortcut.

default CharSequence getContentDescription()

Retrieve the content description associated with this menu item.

abstract int getGroupId()

Return the group identifier that this menu item is part of.

abstract Drawable getIcon()

Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before).

default ColorStateList getIconTintList()
default PorterDuff.Mode getIconTintMode()

Returns the blending mode used to apply the tint to this item's icon, if specified.

abstract Intent getIntent()

Return the Intent associated with this item.

abstract int getItemId()

Return the identifier for this menu item.

abstract ContextMenu.ContextMenuInfo getMenuInfo()

Gets the extra information linked to this menu item.

default int getNumericModifiers()

Return the modifiers for this menu item's numeric (12-key) shortcut.

abstract char getNumericShortcut()

Return the char for this menu item's numeric (12-key) shortcut.

abstract int getOrder()

Return the category and order within the category of this item.

abstract SubMenu getSubMenu()

Get the sub-menu to be invoked when this item is selected, if it has one.

abstract CharSequence getTitle()

Retrieve the current title of the item.

abstract CharSequence getTitleCondensed()

Retrieve the current condensed title of the item.

default CharSequence getTooltipText()

Retrieve the tooltip text associated with this menu item.

abstract boolean hasSubMenu()

Check whether this item has an associated sub-menu.

abstract boolean isActionViewExpanded()

Returns true if this menu item's action view has been expanded.

abstract boolean isCheckable()

Return whether the item can currently display a check mark.

abstract boolean isChecked()

Return whether the item is currently displaying a check mark.

abstract boolean isEnabled()

Return the enabled state of the menu item.

abstract boolean isVisible()

Return the visibility of the menu item.

abstract MenuItem setActionProvider(ActionProvider actionProvider)

Sets the ActionProvider responsible for creating an action view if the item is placed on the action bar.

abstract MenuItem setActionView(int resId)

Set an action view for this menu item.

abstract MenuItem setActionView(View view)

Set an action view for this menu item.

abstract MenuItem 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.

abstract MenuItem setCheckable(boolean checkable)

Control whether this item can display a check mark.

abstract MenuItem 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.

abstract MenuItem setEnabled(boolean enabled)

Sets whether the menu item is enabled.

abstract MenuItem setIcon(Drawable icon)

Change the icon associated with this item.

abstract MenuItem setIcon(int iconRes)

Change the icon associated with this item.

default MenuItem setIconTintList(ColorStateList tint)

Applies a tint to this item's icon.

default MenuItem setIconTintMode(PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setIconTintList(ColorStateList) to this item's icon.

abstract MenuItem setIntent(Intent intent)

Change the Intent associated with this item.

default MenuItem setNumericShortcut(char numericChar, int numericModifiers)

Change the numeric shortcut and modifiers associated with this item.

abstract MenuItem setNumericShortcut(char numericChar)

Change the numeric shortcut associated with this item.

abstract MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener)

Set an MenuItem.OnActionExpandListener on this menu item to be notified when the associated action view is expanded or collapsed.

abstract MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener)

Set a custom listener for invocation of this menu item.

abstract MenuItem 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.

abstract void setShowAsAction(int actionEnum)

Sets how this item should display in the presence of an Action Bar.

abstract MenuItem setShowAsActionFlags(int actionEnum)

Sets how this item should display in the presence of an Action Bar.

abstract MenuItem setTitle(CharSequence title)

Change the title associated with this item.

abstract MenuItem setTitle(int title)

Change the title associated with this item.

abstract MenuItem 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.

abstract MenuItem setVisible(boolean visible)

Sets the visibility of the menu item.

XML attributes

android:iconTint

Tint to apply to the icon.

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

Related methods:

android:iconTintMode

Blending mode used to apply the icon tint.

Must be one of the following constant values.

ConstantValueDescription
add10Combines the tint and icon color and alpha channels, clamping the result to valid color values. Saturate(S + D)
multiplyeMultiplies the color and alpha channels of the icon with those of the tint. [Sa * Da, Sc * Dc]
screenf[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
src_atop9The tint is drawn above the icon, but with the icon’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]
src_in5The tint is masked by the alpha channel of the icon. The icon’s color channels are thrown out. [Sa * Da, Sc * Da]
src_over3The tint is drawn on top of the icon. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]

Related methods:

Constants

SHOW_AS_ACTION_ALWAYS

added in API level 11
int SHOW_AS_ACTION_ALWAYS

Always 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.

Constant Value: 2 (0x00000002)

SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

added in API level 14
int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

This item's action view collapses to a normal menu item. When expanded, the action view temporarily takes over a larger segment of its container.

Constant Value: 8 (0x00000008)

SHOW_AS_ACTION_IF_ROOM

added in API level 11
int SHOW_AS_ACTION_IF_ROOM

Show this item as a button in an Action Bar if the system decides there is room for it.

Constant Value: 1 (0x00000001)

SHOW_AS_ACTION_NEVER

added in API level 11
int SHOW_AS_ACTION_NEVER

Never show this item as a button in an Action Bar.

Constant Value: 0 (0x00000000)

SHOW_AS_ACTION_WITH_TEXT

added in API level 11
int SHOW_AS_ACTION_WITH_TEXT

When this item is in the action bar, always show it with a text label even if it also has an icon specified.

Constant Value: 4 (0x00000004)

Public methods

collapseActionView

added in API level 14
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 flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW. If a listener has been set using setOnActionExpandListener(OnActionExpandListener) it will have its onMenuItemActionCollapse(MenuItem) method invoked. The listener may return false from this method to prevent collapsing the action view.

Returns
boolean true if the action view was collapsed, false otherwise.

expandActionView

added in API level 14
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 flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW. If a listener has been set using setOnActionExpandListener(OnActionExpandListener) it will have its onMenuItemActionExpand(MenuItem) method invoked. The listener may return false from this method to prevent expanding the action view.

Returns
boolean true if the action view was expanded, false otherwise.

getActionProvider

added in API level 14
ActionProvider getActionProvider ()

Gets the ActionProvider.

Returns
ActionProvider The action provider.

See also:

getActionView

added in API level 11
View getActionView ()

Returns the currently set action view for this menu item.

Returns
View This item's action view

See also:

getAlphabeticModifiers

added in API level 26
int getAlphabeticModifiers ()

Return the modifier for this menu item's alphabetic shortcut. The modifier is a combination of META_META_ON, META_CTRL_ON, META_ALT_ON, META_SHIFT_ON, META_SYM_ON, META_FUNCTION_ON. For example, META_FUNCTION_ON|META_CTRL_ON

Returns
int Modifier associated with the keyboard shortcut.

getAlphabeticShortcut

added in API level 1
char getAlphabeticShortcut ()

Return the char for this menu item's alphabetic shortcut.

Returns
char Alphabetic character to use as a shortcut.

getContentDescription

added in API level 26
CharSequence getContentDescription ()

Retrieve the content description associated with this menu item.

Returns
CharSequence The content description.

getGroupId

added in API level 1
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
int The menu item's group identifier.

getIcon

added in API level 1
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 call setIconTintList(ColorStateList) or setIconTintMode(PorterDuff.Mode) on this item, and you use a custom menu presenter in your application, you have to apply the tinting explicitly on the Drawable returned by this method.

Returns
Drawable The icon as a Drawable.

getIconTintList

added in API level 26
ColorStateList getIconTintList ()

Related XML Attributes:

Returns
ColorStateList the tint applied to this item's icon

This value may be null.

See also:

getIconTintMode

added in API level 26
PorterDuff.Mode getIconTintMode ()

Returns the blending mode used to apply the tint to this item's icon, if specified.

Related XML Attributes:

Returns
PorterDuff.Mode the blending mode used to apply the tint to this item's icon

This value may be null.

See also:

getIntent

added in API level 1
Intent getIntent ()

Return the Intent associated with this item. This returns a reference to the Intent which you can change as desired to modify what the Item is holding.

Returns
Intent Returns the last value supplied to setIntent(Intent), or null.

See also:

getItemId

added in API level 1
int getItemId ()

Return the identifier for this menu item. The identifier can not be changed after the menu is created.

Returns
int The menu item's identifier.

getMenuInfo

added in API level 1
ContextMenu.ContextMenuInfo 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
ContextMenu.ContextMenuInfo The extra information linked to the View that added this menu item to the menu. This can be null.

See also:

getNumericModifiers

added in API level 26
int getNumericModifiers ()

Return the modifiers for this menu item's numeric (12-key) shortcut. The modifier is a combination of META_META_ON, META_CTRL_ON, META_ALT_ON, META_SHIFT_ON, META_SYM_ON, META_FUNCTION_ON. For example, META_FUNCTION_ON|META_CTRL_ON

Returns
int Modifier associated with the numeric shortcut.

getNumericShortcut

added in API level 1
char getNumericShortcut ()

Return the char for this menu item's numeric (12-key) shortcut.

Returns
char Numeric character to use as a shortcut.

getOrder

added in API level 1
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 CATEGORY_SYSTEM, CATEGORY_SECONDARY, CATEGORY_ALTERNATIVE, CATEGORY_CONTAINER. See Menu for a full list.

Returns
int The order of this item.

getSubMenu

added in API level 1
SubMenu getSubMenu ()

Get the sub-menu to be invoked when this item is selected, if it has one. See hasSubMenu().

Returns
SubMenu The associated menu if there is one, else null

getTitle

added in API level 1
CharSequence getTitle ()

Retrieve the current title of the item.

Returns
CharSequence The title.

getTitleCondensed

added in API level 1
CharSequence getTitleCondensed ()

Retrieve the current condensed title of the item. If a condensed title was never set, it will return the normal title.

Returns
CharSequence The condensed title, if it exists. Otherwise the normal title.

getTooltipText

added in API level 26
CharSequence getTooltipText ()

Retrieve the tooltip text associated with this menu item.

Returns
CharSequence The tooltip text.

hasSubMenu

added in API level 1
boolean hasSubMenu ()

Check whether this item has an associated sub-menu. I.e. it is a sub-menu of another menu.

Returns
boolean If true this item has a menu; else it is a normal item.

isActionViewExpanded

added in API level 14
boolean isActionViewExpanded ()

Returns true if this menu item's action view has been expanded.

Returns
boolean true if the item's action view is expanded, false otherwise.

See also:

isCheckable

added in API level 1
boolean isCheckable ()

Return whether the item can currently display a check mark.

Returns
boolean If a check mark can be displayed, returns true.

See also:

isChecked

added in API level 1
boolean isChecked ()

Return whether the item is currently displaying a check mark.

Returns
boolean If a check mark is displayed, returns true.

See also:

isEnabled

added in API level 1
boolean isEnabled ()

Return the enabled state of the menu item.

Returns
boolean If true the item is enabled and hence invokable; else it is not.

isVisible

added in API level 1
boolean isVisible ()

Return the visibility of the menu item.

Returns
boolean If true the item is visible; else it is hidden.

setActionProvider

added in API level 14
MenuItem setActionProvider (ActionProvider actionProvider)

Sets the ActionProvider 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(int) or setActionView(View).

Parameters
actionProvider ActionProvider: The action provider.

Returns
MenuItem This Item so additional setters can be called.

See also:

setActionView

added in API level 11
MenuItem setActionView (int resId)

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
resId int: Layout resource to use for presenting this item to the user.

Returns
MenuItem This Item so additional setters can be called.

See also:

setActionView

added in API level 11
MenuItem setActionView (View view)

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: View to use for presenting this item to the user.

Returns
MenuItem This Item so additional setters can be called.

See also:

setAlphabeticShortcut

added in API level 1
MenuItem setAlphabeticShortcut (char alphaChar)

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 is 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 char: The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.

Returns
MenuItem This Item so additional setters can be called.

setAlphabeticShortcut

added in API level 26
MenuItem setAlphabeticShortcut (char alphaChar, 
                int alphaModifiers)

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 char: The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.

alphaModifiers int: The modifier associated with the shortcut. It should be a combination of META_META_ON, META_CTRL_ON, META_ALT_ON, META_SHIFT_ON, META_SYM_ON, META_FUNCTION_ON.

Returns
MenuItem This Item so additional setters can be called.

setCheckable

added in API level 1
MenuItem setCheckable (boolean checkable)

Control whether this item can display a check mark. Setting this does not actually display a check mark (see setChecked(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 boolean: Set to true to allow a check mark, false to disallow. The default is false.

Returns
MenuItem This Item so additional setters can be called.

See also:

setChecked

added in API level 1
MenuItem setChecked (boolean checked)

Control whether this item is shown with a check mark. Note that you must first have enabled checking with setCheckable(boolean) or else the check mark will not appear. If this item is a member of a group that contains mutually-exclusive items (set via 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 boolean: Set to true to display a check mark, false to hide it. The default value is false.

Returns
MenuItem This Item so additional setters can be called.

See also:

setContentDescription

added in API level 26
MenuItem setContentDescription (CharSequence contentDescription)

Change the content description associated with this menu item.

Parameters
contentDescription CharSequence: The new content description.

Returns
MenuItem

setEnabled

added in API level 1
MenuItem setEnabled (boolean enabled)

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 boolean: If true then the item will be invokable; if false it is won't be invokable.

Returns
MenuItem This Item so additional setters can be called.

setIcon

added in API level 1
MenuItem setIcon (Drawable icon)

Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons.

Parameters
icon Drawable: The new icon (as a Drawable) to be displayed.

Returns
MenuItem This Item so additional setters can be called.

setIcon

added in API level 1
MenuItem setIcon (int iconRes)

Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons.

This method will set the resource ID of the icon which will be used to lazily get the Drawable when this item is being shown.

Parameters
iconRes int: The new icon (as a resource ID) to be displayed.

Returns
MenuItem This Item so additional setters can be called.

setIconTintList

added in API level 26
MenuItem setIconTintList (ColorStateList tint)

Applies a tint to this item's icon. Does not modify the current tint mode, which is SRC_IN by default.

Subsequent calls to setIcon(Drawable) or setIcon(int) will automatically mutate the icon and apply the specified tint and tint mode using setTintList(ColorStateList).

Related XML Attributes:

Parameters
tint ColorStateList: the tint to apply, may be null to clear tint

Returns
MenuItem

See also:

setIconTintMode

added in API level 26
MenuItem setIconTintMode (PorterDuff.Mode tintMode)

Specifies the blending mode used to apply the tint specified by setIconTintList(ColorStateList) to this item's icon. The default mode is SRC_IN.

Related XML Attributes:

Parameters
tintMode PorterDuff.Mode: the blending mode used to apply the tint, may be null to clear tint

Returns
MenuItem

See also:

setIntent

added in API level 1
MenuItem setIntent (Intent intent)

Change the Intent associated with this item. By default there is no Intent associated with a menu item. If you set one, and nothing else handles the item, then the default behavior will be to call startActivity(Intent) with the given Intent.

Note that setIntent() can not be used with the versions of add(int) that take a Runnable, because run() does not return a value so there is no way to tell if it handled the item. In this case it is assumed that the Runnable always handles the item, and the intent will never be started.

Parameters
intent Intent: The Intent to associated with the item. This Intent object is not copied, so be careful not to modify it later.

Returns
MenuItem This Item so additional setters can be called.

See also:

setNumericShortcut

added in API level 26
MenuItem setNumericShortcut (char numericChar, 
                int numericModifiers)

Change the numeric shortcut and modifiers associated with this item.

See Menu for the menu types that support shortcuts.

Parameters
numericChar char: The numeric shortcut key. This is the shortcut when using a 12-key (numeric) keyboard.

numericModifiers int: The modifier associated with the shortcut. It should be a combination of META_META_ON, META_CTRL_ON, META_ALT_ON, META_SHIFT_ON, META_SYM_ON, META_FUNCTION_ON.

Returns
MenuItem This Item so additional setters can be called.

setNumericShortcut

added in API level 1
MenuItem setNumericShortcut (char numericChar)

Change the numeric shortcut associated with this item.

See Menu for the menu types that support shortcuts.

Parameters
numericChar char: The numeric shortcut key. This is the shortcut when using a 12-key (numeric) keyboard.

Returns
MenuItem This Item so additional setters can be called.

setOnActionExpandListener

added in API level 14
MenuItem setOnActionExpandListener (MenuItem.OnActionExpandListener listener)

Set an MenuItem.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 flag SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW.

Parameters
listener MenuItem.OnActionExpandListener: Listener that will respond to expand/collapse events

Returns
MenuItem This menu item instance for call chaining

setOnMenuItemClickListener

added in API level 1
MenuItem setOnMenuItemClickListener (MenuItem.OnMenuItemClickListener menuItemClickListener)

Set a custom listener for invocation of this menu item. In most situations, it is more efficient and easier to use onOptionsItemSelected(MenuItem) or onContextItemSelected(MenuItem).

Parameters
menuItemClickListener MenuItem.OnMenuItemClickListener: The object to receive invokations.

Returns
MenuItem This Item so additional setters can be called.

See also:

setShortcut

added in API level 1
MenuItem setShortcut (char numericChar, 
                char alphaChar)

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 is 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 char: The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard.

alphaChar char: The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.

Returns
MenuItem This Item so additional setters can be called.

setShortcut

added in API level 26
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 char: The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard.

alphaChar char: The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.

numericModifiers int: The numeric modifier associated with the shortcut. It should be a combination of META_META_ON, META_CTRL_ON, META_ALT_ON, META_SHIFT_ON, META_SYM_ON, META_FUNCTION_ON.

alphaModifiers int: The alphabetic modifier associated with the shortcut. It should be a combination of META_META_ON, META_CTRL_ON, META_ALT_ON, META_SHIFT_ON, META_SYM_ON, META_FUNCTION_ON.

Returns
MenuItem This Item so additional setters can be called.

setShowAsAction

added in API level 11
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 of SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER should be used, and you may optionally OR the value with SHOW_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 int: How the item should display. One of SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER. SHOW_AS_ACTION_NEVER is the default.

See also:

setShowAsActionFlags

added in API level 14
MenuItem setShowAsActionFlags (int actionEnum)

Sets how this item should display in the presence of an Action Bar. The parameter actionEnum is a flag set. One of SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER should be used, and you may optionally OR the value with SHOW_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 int: How the item should display. One of SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER. SHOW_AS_ACTION_NEVER is the default.

Returns
MenuItem This MenuItem instance for call chaining.

See also:

setTitle

added in API level 1
MenuItem setTitle (CharSequence title)

Change the title associated with this item.

Parameters
title CharSequence: The new text to be displayed.

Returns
MenuItem This Item so additional setters can be called.

setTitle

added in API level 1
MenuItem setTitle (int title)

Change the title associated with this item.

Some menu types do not sufficient space to show the full title, and instead a condensed title is preferred. See Menu for more information.

Parameters
title int: The resource id of the new text to be displayed.

Returns
MenuItem This Item so additional setters can be called.

See also:

setTitleCondensed

added in API level 1
MenuItem setTitleCondensed (CharSequence title)

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 CharSequence: The new text to be displayed as the condensed title.

Returns
MenuItem This Item so additional setters can be called.

setTooltipText

added in API level 26
MenuItem setTooltipText (CharSequence tooltipText)

Change the tooltip text associated with this menu item.

Parameters
tooltipText CharSequence: The new tooltip text.

Returns
MenuItem

setVisible

added in API level 1
MenuItem setVisible (boolean visible)

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 and disabled).

Parameters
visible boolean: If true then the item will be visible; if false it is hidden.

Returns
MenuItem This Item so additional setters can be called.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)