MenuItemCompat

Added in 1.1.0

public final class 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 , but take a MenuItem object as an additional argument.

Summary

Nested types

This interface is deprecated.

Use MenuItem.OnActionExpandListener directly.

Constants

static final int

This field is deprecated.

Use SHOW_AS_ACTION_ALWAYS directly.

static final int

This field is deprecated.

Use SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW directly.

static final int

This field is deprecated.

Use SHOW_AS_ACTION_IF_ROOM directly.

static final int

This field is deprecated.

Use SHOW_AS_ACTION_NEVER directly.

static final int

This field is deprecated.

Use SHOW_AS_ACTION_WITH_TEXT directly.

Public methods

static boolean
@ReplaceWith(expression = "item.collapseActionView()")
collapseActionView(MenuItem item)

This method is deprecated.

Use collapseActionView directly.

static boolean
@ReplaceWith(expression = "item.expandActionView()")
expandActionView(MenuItem item)

This method is deprecated.

Use expandActionView directly.

static @Nullable ActionProvider

Gets the ActionProvider.

static View
@ReplaceWith(expression = "item.getActionView()")
getActionView(MenuItem item)

This method is deprecated.

Use getActionView directly.

static int

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

static @Nullable CharSequence

Retrieve the content description associated with this menu item.

static @Nullable ColorStateList
static @Nullable PorterDuff.Mode

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

static int

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

static @Nullable CharSequence

Retrieve the tooltip text associated with this menu item.

static boolean
@ReplaceWith(expression = "item.isActionViewExpanded()")
isActionViewExpanded(MenuItem item)

This method is deprecated.

Use isActionViewExpanded directly.

static @Nullable MenuItem
setActionProvider(
    @NonNull MenuItem item,
    @Nullable ActionProvider provider
)

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

static MenuItem
@ReplaceWith(expression = "item.setActionView(resId)")
setActionView(MenuItem item, int resId)

This method is deprecated.

Use setActionView directly.

static MenuItem
@ReplaceWith(expression = "item.setActionView(view)")
setActionView(MenuItem item, View view)

This method is deprecated.

Use setActionView directly.

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

Change the alphabetic shortcut associated with this item.

static void
setContentDescription(
    @NonNull MenuItem item,
    @Nullable CharSequence contentDescription
)

Change the content description associated with this menu item.

static void

Applies a tint to the item's icon.

static void

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

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

Change the numeric shortcut and modifiers associated with this item.

static MenuItem

This method is deprecated.

Use setOnActionExpandListener directly.

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

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

static void
@ReplaceWith(expression = "item.setShowAsAction(actionEnum)")
setShowAsAction(MenuItem item, int actionEnum)

This method is deprecated.

Use setShowAsAction directly.

static void

Change the tooltip text associated with this menu item.

Constants

SHOW_AS_ACTION_ALWAYS

Added in 1.1.0
Deprecated in 1.1.0
public static final int SHOW_AS_ACTION_ALWAYS = 2

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.

SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

Added in 1.1.0
Deprecated in 1.1.0
public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8

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.

SHOW_AS_ACTION_IF_ROOM

Added in 1.1.0
Deprecated in 1.1.0
public static final int SHOW_AS_ACTION_IF_ROOM = 1

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

SHOW_AS_ACTION_NEVER

Added in 1.1.0
Deprecated in 1.1.0
public static final int SHOW_AS_ACTION_NEVER = 0

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

SHOW_AS_ACTION_WITH_TEXT

Added in 1.1.0
Deprecated in 1.1.0
public static final int SHOW_AS_ACTION_WITH_TEXT = 4

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

collapseActionView

Added in 1.1.0
Deprecated in 1.1.0
@ReplaceWith(expression = "item.collapseActionView()")
public static boolean collapseActionView(MenuItem item)

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 it will have its onMenuItemActionCollapse 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 1.1.0
Deprecated in 1.1.0
@ReplaceWith(expression = "item.expandActionView()")
public static boolean expandActionView(MenuItem item)

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 it will have its onMenuItemActionExpand 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 1.1.0
public static @Nullable ActionProvider getActionProvider(@NonNull MenuItem item)

Gets the ActionProvider.

Returns
@Nullable ActionProvider

The action provider.

getActionView

Added in 1.1.0
Deprecated in 1.1.0
@ReplaceWith(expression = "item.getActionView()")
public static View getActionView(MenuItem item)

Returns the currently set action view for this menu item.

Parameters
MenuItem item

the item to query

Returns
View

This item's action view

getAlphabeticModifiers

Added in 1.1.0
public static int getAlphabeticModifiers(@NonNull 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

Returns
int

Modifier associated with the keyboard shortcut.

getContentDescription

Added in 1.1.0
public static @Nullable CharSequence getContentDescription(@NonNull MenuItem item)

Retrieve the content description associated with this menu item.

Returns
@Nullable CharSequence

The content description.

getIconTintList

Added in 1.1.0
public static @Nullable ColorStateList getIconTintList(@NonNull MenuItem item)
Returns
@Nullable ColorStateList

the tint applied to the item's icon

See also
setIconTintList

getIconTintMode

Added in 1.1.0
public static @Nullable PorterDuff.Mode