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

This method is deprecated.

Use collapseActionView directly.

static boolean

This method is deprecated.

Use expandActionView directly.

static @Nullable ActionProvider

Gets the ActionProvider.

static View

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

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
setActionView(MenuItem item, int resId)

This method is deprecated.

Use setActionView directly.

static MenuItem
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
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
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
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
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 getIconTintMode(@NonNull MenuItem item)

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

Returns
@Nullable PorterDuff.Mode

the blending mode used to apply the tint to the item's icon

See also
setIconTintMode

getNumericModifiers

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

Returns
int

Modifier associated with the numeric shortcut.

getTooltipText

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

Retrieve the tooltip text associated with this menu item.

Returns
@Nullable CharSequence

The tooltip text.

isActionViewExpanded

Added in 1.1.0
Deprecated in 1.1.0
public static boolean isActionViewExpanded(MenuItem item)

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.

setActionProvider

Added in 1.1.0
public 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. 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.

Parameters
@NonNull MenuItem item

item to change

@Nullable ActionProvider provider

The action provider.

Returns
@Nullable MenuItem

This Item so additional setters can be called.

See also
ActionProvider

setActionView

Added in 1.1.0
Deprecated in 1.1.0
public static MenuItem setActionView(MenuItem item, 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.

Parameters
MenuItem item

the item to change

int resId

Layout resource to use for presenting this item to the user.

Returns
MenuItem

This Item so additional setters can be called.

See also
setShowAsAction

setActionView

Added in 1.1.0
Deprecated in 1.1.0
public static MenuItem setActionView(MenuItem item, 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.

Parameters
MenuItem item

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
setShowAsAction

setAlphabeticShortcut

Added in 1.1.0
public static void setAlphabeticShortcut(
    @NonNull 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
@NonNull MenuItem item

item for which to set the shortcut.

char alphaChar

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

int alphaModifiers

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 1.1.0
public static void setContentDescription(
    @NonNull MenuItem item,
    @Nullable CharSequence contentDescription
)

Change the content description associated with this menu item.

Parameters
@NonNull MenuItem item

item to change.

@Nullable CharSequence contentDescription

The new content description.

setIconTintList

Added in 1.1.0
public static void setIconTintList(@NonNull MenuItem item, @Nullable 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 or setIcon will automatically mutate the icon and apply the specified tint and tint mode.

Parameters
@NonNull MenuItem item

item for which to set the tint.

@Nullable ColorStateList tint

the tint to apply, may be null to clear tint

See also
getIconTintList

setIconTintMode

Added in 1.1.0
public static void setIconTintMode(@NonNull MenuItem item, @Nullable PorterDuff.Mode tintMode)

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

Parameters
@NonNull MenuItem item

item for which to set the tint mode.

@Nullable PorterDuff.Mode tintMode

the blending mode used to apply the tint, may be null to clear tint

See also
setIconTintList

setNumericShortcut

Added in 1.1.0
public static void setNumericShortcut(
    @NonNull 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
@NonNull MenuItem item

item for which to set the shortcut.

char numericChar

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

int numericModifiers

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 1.1.0
Deprecated in 1.1.0
public static MenuItem setOnActionExpandListener(
    MenuItem item,
    MenuItemCompat.OnActionExpandListener listener
)

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

item on which to set the listener.

MenuItemCompat.OnActionExpandListener listener

Listener that will respond to expand/collapse events

Returns
MenuItem

This menu item instance for call chaining

setShortcut

Added in 1.1.0
public 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. 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
@NonNull MenuItem item

item for which to set the shortcut.

char numericChar

The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard.

char alphaChar

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

int numericModifiers

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.

int alphaModifiers

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 1.1.0
Deprecated in 1.1.0
public static void setShowAsAction(MenuItem item, int actionEnum)

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.

Parameters
MenuItem item

- the item to change

int actionEnum

- How the item should display.

setTooltipText

Added in 1.1.0
public static void setTooltipText(@NonNull MenuItem item, @Nullable CharSequence tooltipText)

Change the tooltip text associated with this menu item.

Parameters
@NonNull MenuItem item

item to change.

@Nullable CharSequence tooltipText

The new tooltip text