MenuItemCompat
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 MenuItem, but take a MenuItem object as an additional argument.
Summary
Nested types |
|---|
public interface MenuItemCompat.OnActionExpandListenerThis interface is deprecated. Use |
Constants |
|
|---|---|
static final int |
This field is deprecated. Use |
static final int |
This field is deprecated. Use |
static final int |
This field is deprecated. Use |
static final int |
This field is deprecated. Use |
static final int |
This field is deprecated. Use |
Public methods |
|
|---|---|
static boolean |
@ReplaceWith(expression = "item.collapseActionView()")This method is deprecated. Use |
static boolean |
@ReplaceWith(expression = "item.expandActionView()")This method is deprecated. Use |
static @Nullable ActionProvider |
getActionProvider(@NonNull MenuItem item)Gets the |
static View |
@ReplaceWith(expression = "item.getActionView()")This method is deprecated. Use |
static int |
Return the modifier for this menu item's alphabetic shortcut. |
static @Nullable CharSequence |
getContentDescription(@NonNull MenuItem item)Retrieve the content description associated with this menu item. |
static @Nullable ColorStateList |
getIconTintList(@NonNull MenuItem item) |
static @Nullable PorterDuff.Mode |
getIconTintMode(@NonNull MenuItem item)Returns the blending mode used to apply the tint to the item's icon, if specified. |
static int |
getNumericModifiers(@NonNull MenuItem item)Return the modifiers for this menu item's numeric (12-key) shortcut. |
static @Nullable CharSequence |
getTooltipText(@NonNull MenuItem item)Retrieve the tooltip text associated with this menu item. |
static boolean |
@ReplaceWith(expression = "item.isActionViewExpanded()")This method is deprecated. Use |
static @Nullable MenuItem |
setActionProvider(Sets the |
static MenuItem |
@ReplaceWith(expression = "item.setActionView(resId)")This method is deprecated. Use |
static MenuItem |
@ReplaceWith(expression = "item.setActionView(view)")This method is deprecated. Use |
static void |
setAlphabeticShortcut(Change the alphabetic shortcut associated with this item. |
static void |
setContentDescription(Change the content description associated with this menu item. |
static void |
setIconTintList(@NonNull MenuItem item, @Nullable ColorStateList tint)Applies a tint to the item's icon. |
static void |
setIconTintMode(@NonNull MenuItem item, @Nullable PorterDuff.Mode tintMode)Specifies the blending mode used to apply the tint specified by |
static void |
setNumericShortcut(Change the numeric shortcut and modifiers associated with this item. |
static MenuItem |
This method is deprecated. Use |
static void |
setShortcut(Change both the numeric and alphabetic shortcut associated with this item. |
static void |
@ReplaceWith(expression = "item.setShowAsAction(actionEnum)")This method is deprecated. Use |
static void |
setTooltipText(@NonNull MenuItem item, @Nullable CharSequence tooltipText)Change the tooltip text associated with this menu item. |
Constants
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.
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.
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.
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
@ReplaceWith(expression = "item.collapseActionView()")
public static booleancollapseActionView(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. |
@ReplaceWith(expression = "item.expandActionView()")
public static booleanexpandActionView(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
public static @Nullable ActionProvider getActionProvider(@NonNull MenuItem item)
Gets the ActionProvider.
| Returns | |
|---|---|
@Nullable ActionProvider |
The action provider. |
| See also | |
|---|---|
ActionProvider |
|
setActionProvider |
@ReplaceWith(expression = "item.getActionView()")
public static ViewgetActionView(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
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
public static @Nullable CharSequence getContentDescription(@NonNull MenuItem item)
Retrieve the content description associated with this menu item.
| Returns | |
|---|---|
@Nullable CharSequence |
The content description. |
getIconTintList
public static @Nullable ColorStateList getIconTintList(@NonNull MenuItem item)
| Returns | |
|---|---|
@Nullable ColorStateList |
the tint applied to the item's icon |
| See also | |
|---|---|
setIconTintList |
getIconTintMode
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
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
public static @Nullable CharSequence getTooltipText(@NonNull MenuItem item)
Retrieve the tooltip text associated with this menu item.
| Returns | |
|---|---|
@Nullable CharSequence |
The tooltip text. |
@ReplaceWith(expression = "item.isActionViewExpanded()")
public static booleanisActionViewExpanded(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
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. |
| See also | |
|---|---|
ActionProvider |
@ReplaceWith(expression = "item.setActionView(resId)")
public static MenuItemsetActionView(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 |
@ReplaceWith(expression = "item.setActionView(view)")
public static MenuItemsetActionView(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
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 |
setContentDescription
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
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 |
| See also | |
|---|---|
getIconTintList |
setIconTintMode
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 |
| See also | |
|---|---|
setIconTintList |
setNumericShortcut
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 |
public static MenuItemsetOnActionExpandListener(
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
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 |
int alphaModifiers |
The alphabetic modifier associated with the shortcut. It should be a combination of |
@ReplaceWith(expression = "item.setShowAsAction(actionEnum)")
public static voidsetShowAsAction(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
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 |