Skip to content

Most visited

Recently visited

navigation

MenuItemCompat

public final class MenuItemCompat
extends Object

java.lang.Object
   ↳ android.support.v4.view.MenuItemCompat


Helper for accessing features in MenuItem.

Note: You cannot get an instance of this class. Instead, it provides static methods that correspond to the methods in MenuItem, but take a MenuItem object as an additional argument.

Summary

Nested classes

interface MenuItemCompat.OnActionExpandListener

This interface was deprecated in API level 26.0.0. Use MenuItem.OnActionExpandListener directly.  

Constants

int SHOW_AS_ACTION_ALWAYS

This constant was deprecated in API level 26.0.0. Use SHOW_AS_ACTION_ALWAYS directly.

int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

This constant was deprecated in API level 26.0.0. Use SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW directly.

int SHOW_AS_ACTION_IF_ROOM

This constant was deprecated in API level 26.0.0. Use SHOW_AS_ACTION_IF_ROOM directly.

int SHOW_AS_ACTION_NEVER

This constant was deprecated in API level 26.0.0. Use SHOW_AS_ACTION_NEVER directly.

int SHOW_AS_ACTION_WITH_TEXT

This constant was deprecated in API level 26.0.0. Use SHOW_AS_ACTION_WITH_TEXT directly.

Public methods

static boolean collapseActionView(MenuItem item)

This method was deprecated in API level 26.0.0. Use collapseActionView() directly.

static boolean expandActionView(MenuItem item)

This method was deprecated in API level 26.0.0. Use expandActionView() directly.

static ActionProvider getActionProvider(MenuItem item)

Gets the ActionProvider.

static View getActionView(MenuItem item)

This method was deprecated in API level 26.0.0. Use getActionView() directly.

static int getAlphabeticModifiers(MenuItem item)

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

static CharSequence getContentDescription(MenuItem item)

Retrieve the content description associated with this menu item.

static ColorStateList getIconTintList(MenuItem item)
static PorterDuff.Mode getIconTintMode(MenuItem item)

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

static int getNumericModifiers(MenuItem item)

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

static CharSequence getTooltipText(MenuItem item)

Retrieve the tooltip text associated with this menu item.

static boolean isActionViewExpanded(MenuItem item)

This method was deprecated in API level 26.0.0. Use isActionViewExpanded() directly.

static MenuItem setActionProvider(MenuItem item, ActionProvider provider)

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

static MenuItem setActionView(MenuItem item, int resId)

This method was deprecated in API level 26.0.0. Use setActionView(int) directly.

static MenuItem setActionView(MenuItem item, View view)

This method was deprecated in API level 26.0.0. Use setActionView(View) directly.

static void setAlphabeticShortcut(MenuItem item, char alphaChar, int alphaModifiers)

Change the alphabetic shortcut associated with this item.

static void setContentDescription(MenuItem item, CharSequence contentDescription)

Change the content description associated with this menu item.

static void setIconTintList(MenuItem item, ColorStateList tint)

Applies a tint to the item's icon.

static void setIconTintMode(MenuItem item, PorterDuff.Mode tintMode)

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

static void setNumericShortcut(MenuItem item, char numericChar, int numericModifiers)

Change the numeric shortcut and modifiers associated with this item.

static MenuItem setOnActionExpandListener(MenuItem item, MenuItemCompat.OnActionExpandListener listener)

This method was deprecated in API level 26.0.0. Use setOnActionExpandListener(MenuItem.OnActionExpandListener) directly.

static void setShortcut(MenuItem item, char numericChar, char alphaChar, int numericModifiers, int alphaModifiers)

Change both the numeric and alphabetic shortcut associated with this item.

static void setShowAsAction(MenuItem item, int actionEnum)

This method was deprecated in API level 26.0.0. Use setShowAsAction(int) directly.

static void setTooltipText(MenuItem item, CharSequence tooltipText)

Change the tooltip text associated with this menu item.

Inherited methods

From class java.lang.Object

Constants

SHOW_AS_ACTION_ALWAYS

added in version 22.0.0
int SHOW_AS_ACTION_ALWAYS

This constant was deprecated in API level 26.0.0.
Use SHOW_AS_ACTION_ALWAYS directly.

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 version 22.0.0
int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

This constant was deprecated in API level 26.0.0.
Use SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW directly.

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 version 22.0.0
int SHOW_AS_ACTION_IF_ROOM

This constant was deprecated in API level 26.0.0.
Use SHOW_AS_ACTION_IF_ROOM directly.

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 version 22.0.0
int SHOW_AS_ACTION_NEVER

This constant was deprecated in API level 26.0.0.
Use SHOW_AS_ACTION_NEVER directly.

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

Constant Value: 0 (0x00000000)

SHOW_AS_ACTION_WITH_TEXT

added in version 22.0.0
int SHOW_AS_ACTION_WITH_TEXT

This constant was deprecated in API level 26.0.0.
Use SHOW_AS_ACTION_WITH_TEXT directly.

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 version 22.0.0
boolean collapseActionView (MenuItem item)

This method was deprecated in API level 26.0.0.
Use collapseActionView() directly.

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(MenuItem, android.support.v4.view.MenuItemCompat.OnActionExpandListener) it will have its onMenuItemActionCollapse(MenuItem) method invoked. The listener may return false from this method to prevent collapsing the action view.

Parameters
item MenuItem

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

expandActionView

added in version 22.0.0
boolean expandActionView (MenuItem item)

This method was deprecated in API level 26.0.0.
Use expandActionView() directly.

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(MenuItem, OnActionExpandListener) it will have its onMenuItemActionExpand(MenuItem) method invoked. The listener may return false from this method to prevent expanding the action view.

Parameters
item MenuItem

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

getActionProvider

added in version 22.0.0
ActionProvider getActionProvider (MenuItem item)

Gets the ActionProvider.

Parameters
item MenuItem

Returns
ActionProvider The action provider.

See also:

getActionView

added in version 22.0.0
View getActionView (MenuItem item)

This method was deprecated in API level 26.0.0.
Use getActionView() directly.

Returns the currently set action view for this menu item.

Parameters
item MenuItem: the item to query

Returns
View This item's action view

getAlphabeticModifiers

added in version 26.0.0
int getAlphabeticModifiers (MenuItem item)

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

Parameters
item MenuItem

Returns
int Modifier associated with the keyboard shortcut.

getContentDescription

added in version 26.0.0
CharSequence getContentDescription (MenuItem item)

Retrieve the content description associated with this menu item.

Parameters
item MenuItem

Returns
CharSequence The content description.

getIconTintList

added in version 26.0.0
ColorStateList getIconTintList (MenuItem item)

Parameters
item MenuItem

Returns
ColorStateList the tint applied to the item's icon

See also:

getIconTintMode

added in version 26.0.0
PorterDuff.Mode getIconTintMode (MenuItem item)

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

Parameters
item MenuItem

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

See also:

getNumericModifiers

added in version 26.0.0
int getNumericModifiers (MenuItem item)

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

Parameters
item MenuItem

Returns
int Modifier associated with the numeric shortcut.

getTooltipText

added in version 26.0.0
CharSequence getTooltipText (MenuItem item)

Retrieve the tooltip text associated with this menu item.

Parameters
item MenuItem

Returns
CharSequence The tooltip text.

isActionViewExpanded

added in version 22.0.0
boolean isActionViewExpanded (MenuItem item)

This method was deprecated in API level 26.0.0.
Use isActionViewExpanded() directly.

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

Parameters
item MenuItem

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

See also:

setActionProvider

added in version 22.0.0
MenuItem setActionProvider (MenuItem item, 
                ActionProvider provider)

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(MenuItem, View).

Parameters
item MenuItem: item to change

provider ActionProvider: The action provider.

Returns
MenuItem This Item so additional setters can be called.

See also:

setActionView

added in version 22.0.0
MenuItem setActionView (MenuItem item, 
                int resId)

This method was deprecated in API level 26.0.0.
Use setActionView(int) directly.

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(MenuItem, ActionProvider).

Parameters
item MenuItem: the item to change

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 version 22.0.0
MenuItem setActionView (MenuItem item, 
                View view)

This method was deprecated in API level 26.0.0.
Use setActionView(View) directly.

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.

Parameters
item MenuItem: the item to change

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 version 26.0.0
void setAlphabeticShortcut (MenuItem item, 
                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
item MenuItem

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.

setContentDescription

added in version 26.0.0
void setContentDescription (MenuItem item, 
                CharSequence contentDescription)

Change the content description associated with this menu item.

Parameters
item MenuItem: item to change.

contentDescription CharSequence: The new content description.

setIconTintList

added in version 26.0.0
void setIconTintList (MenuItem item, 
                ColorStateList tint)

Applies a tint to the item's icon. Does not modify the current tint mode of that item, 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.

Parameters
item MenuItem

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

See also:

setIconTintMode

added in version 26.0.0
void setIconTintMode (MenuItem item, 
                PorterDuff.Mode tintMode)

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

Parameters
item MenuItem

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

See also:

setNumericShortcut

added in version 26.0.0
void setNumericShortcut (MenuItem item, 
                char numericChar, 
                int numericModifiers)

Change the numeric shortcut and modifiers associated with this item.

See Menu for the menu types that support shortcuts.

Parameters
item MenuItem

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.

setOnActionExpandListener

added in version 22.0.0
MenuItem setOnActionExpandListener (MenuItem item, 
                MenuItemCompat.OnActionExpandListener listener)

This method was deprecated in API level 26.0.0.
Use setOnActionExpandListener(MenuItem.OnActionExpandListener) directly.

Set an MenuItemCompat.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
item MenuItem

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

Returns
MenuItem This menu item instance for call chaining

setShortcut

added in version 26.0.0
void setShortcut (MenuItem item, 
                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
item MenuItem

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.

setShowAsAction

added in version 22.0.0
void setShowAsAction (MenuItem item, 
                int actionEnum)

This method was deprecated in API level 26.0.0.
Use setShowAsAction(int) directly.

Sets how this item should display in the presence of a compatible Action Bar. If the given item is compatible, this will call the item's supported implementation of setShowAsAction(int).

Parameters
item MenuItem: - the item to change

actionEnum int: - How the item should display.

setTooltipText

added in version 26.0.0
void setTooltipText (MenuItem item, 
                CharSequence tooltipText)

Change the tooltip text associated with this menu item.

Parameters
item MenuItem: item to change.

tooltipText CharSequence: The new tooltip text

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 one-minute survey?
Help us improve Android tools and documentation.