public final class

MenuItemCompat

extends java.lang.Object

 java.lang.Object

↳androidx.core.view.MenuItemCompat

Gradle dependencies

compile group: 'androidx.core', name: 'core', version: '1.9.0-alpha04'

  • groupId: androidx.core
  • artifactId: core
  • version: 1.9.0-alpha04

Artifact androidx.core:core:1.9.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.core:core com.android.support:support-compat

Androidx class mapping:

androidx.core.view.MenuItemCompat android.support.v4.view.MenuItemCompat

Overview

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

Fields
public static final intSHOW_AS_ACTION_ALWAYS

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

public static final intSHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

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

public static final intSHOW_AS_ACTION_IF_ROOM

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

public static final intSHOW_AS_ACTION_NEVER

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

public static final intSHOW_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.

Methods
public static booleancollapseActionView(MenuItem item)

Collapse the action view associated with this menu item.

public static booleanexpandActionView(MenuItem item)

Expand the action view associated with this menu item.

public static ActionProvidergetActionProvider(MenuItem item)

Gets the ActionProvider.

public static ViewgetActionView(MenuItem item)

Returns the currently set action view for this menu item.

public static intgetAlphabeticModifiers(MenuItem item)

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

public static java.lang.CharSequencegetContentDescription(MenuItem item)

Retrieve the content description associated with this menu item.

public static ColorStateListgetIconTintList(MenuItem item)

public static PorterDuff.ModegetIconTintMode(MenuItem item)

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

public static intgetNumericModifiers(MenuItem item)

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

public static java.lang.CharSequencegetTooltipText(MenuItem item)

Retrieve the tooltip text associated with this menu item.

public static booleanisActionViewExpanded(MenuItem item)

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

public static MenuItemsetActionProvider(MenuItem item, ActionProvider provider)

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

public static MenuItemsetActionView(MenuItem item, int resId)

Set an action view for this menu item.

public static MenuItemsetActionView(MenuItem item, View view)

Set an action view for this menu item.

public static voidsetAlphabeticShortcut(MenuItem item, char alphaChar, int alphaModifiers)

Change the alphabetic shortcut associated with this item.

public static voidsetContentDescription(MenuItem item, java.lang.CharSequence contentDescription)

Change the content description associated with this menu item.

public static voidsetIconTintList(MenuItem item, ColorStateList tint)

Applies a tint to the item's icon.

public static voidsetIconTintMode(MenuItem item, PorterDuff.Mode tintMode)

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

public static voidsetNumericShortcut(MenuItem item, char numericChar, int numericModifiers)

Change the numeric shortcut and modifiers associated with this item.

public static MenuItemsetOnActionExpandListener(MenuItem item, MenuItemCompat.OnActionExpandListener listener)

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

public static voidsetShortcut(MenuItem item, char numericChar, char alphaChar, int numericModifiers, int alphaModifiers)

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

public static voidsetShowAsAction(MenuItem item, int actionEnum)

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

public static voidsetTooltipText(MenuItem item, java.lang.CharSequence tooltipText)

Change the tooltip text associated with this menu item.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final int SHOW_AS_ACTION_NEVER

Deprecated: Use MenuItem directly.

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

public static final int SHOW_AS_ACTION_IF_ROOM

Deprecated: Use MenuItem directly.

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_ALWAYS

Deprecated: Use MenuItem 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.

public static final int SHOW_AS_ACTION_WITH_TEXT

Deprecated: Use MenuItem directly.

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

public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW

Deprecated: Use MenuItem 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.

Methods

public static void setShowAsAction(MenuItem item, int actionEnum)

Deprecated: Use MenuItem 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 MenuItem.

Parameters:

item: - the item to change
actionEnum: - How the item should display.

public static MenuItem setActionView(MenuItem item, View view)

Deprecated: Use MenuItem 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: the item to change
view: View to use for presenting this item to the user.

Returns:

This Item so additional setters can be called.

See also: MenuItemCompat.setShowAsAction(MenuItem, int)

public static MenuItem setActionView(MenuItem item, int resId)

Deprecated: Use MenuItem 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 MenuItemCompat.setActionProvider(MenuItem, ActionProvider).

Parameters:

item: the item to change
resId: Layout resource to use for presenting this item to the user.

Returns:

This Item so additional setters can be called.

See also: MenuItemCompat.setShowAsAction(MenuItem, int)

public static View getActionView(MenuItem item)

Deprecated: Use MenuItem directly.

Returns the currently set action view for this menu item.

Parameters:

item: the item to query

Returns:

This item's action view

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

Parameters:

item: item to change
provider: The action provider.

Returns:

This Item so additional setters can be called.

See also: ActionProvider

public static ActionProvider getActionProvider(MenuItem item)

Gets the ActionProvider.

Returns:

The action provider.

See also: ActionProvider, MenuItemCompat.setActionProvider(MenuItem, ActionProvider)

public static boolean expandActionView(MenuItem item)

Deprecated: Use MenuItem 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 MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW. If a listener has been set using MenuItemCompat.setOnActionExpandListener(MenuItem, MenuItemCompat.OnActionExpandListener) it will have its MenuItemCompat.OnActionExpandListener.onMenuItemActionExpand(MenuItem) method invoked. The listener may return false from this method to prevent expanding the action view.

Returns:

true if the action view was expanded, false otherwise.

public static boolean collapseActionView(MenuItem item)

Deprecated: Use MenuItem 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 MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW. If a listener has been set using MenuItemCompat.setOnActionExpandListener(MenuItem, MenuItemCompat.OnActionExpandListener) it will have its MenuItemCompat.OnActionExpandListener.onMenuItemActionCollapse(MenuItem) method invoked. The listener may return false from this method to prevent collapsing the action view.

Returns:

true if the action view was collapsed, false otherwise.

public static boolean isActionViewExpanded(MenuItem item)

Deprecated: Use MenuItem directly.

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

Returns:

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

See also: MenuItemCompat.expandActionView(MenuItem), MenuItemCompat.collapseActionView(MenuItem), MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW, MenuItemCompat.OnActionExpandListener

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

Deprecated: Use MenuItem 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 MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW.

Parameters:

listener: Listener that will respond to expand/collapse events

Returns:

This menu item instance for call chaining

public static void setContentDescription(MenuItem item, java.lang.CharSequence contentDescription)

Change the content description associated with this menu item.

Parameters:

item: item to change.
contentDescription: The new content description.

public static java.lang.CharSequence getContentDescription(MenuItem item)

Retrieve the content description associated with this menu item.

Returns:

The content description.

public static void setTooltipText(MenuItem item, java.lang.CharSequence tooltipText)

Change the tooltip text associated with this menu item.

Parameters:

item: item to change.
tooltipText: The new tooltip text

public static java.lang.CharSequence getTooltipText(MenuItem item)

Retrieve the tooltip text associated with this menu item.

Returns:

The tooltip text.

public static 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 for the menu types that support shortcuts.

Parameters:

numericChar: The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard.
numericModifiers: The numeric modifier associated with the shortcut. It should be a combination of , , , , , .
alphaChar: The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.
alphaModifiers: The alphabetic modifier associated with the shortcut. It should be a combination of , , , , , .

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

Change the numeric shortcut and modifiers associated with this item.

See for the menu types that support shortcuts.

Parameters:

numericChar: The numeric shortcut key. This is the shortcut when using a 12-key (numeric) keyboard.
numericModifiers: The modifier associated with the shortcut. It should be a combination of , , , , , .

public static int getNumericModifiers(MenuItem item)

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

Returns:

Modifier associated with the numeric shortcut.

public static 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 for the menu types that support shortcuts.

Parameters:

alphaChar: The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys.
alphaModifiers: The modifier associated with the shortcut. It should be a combination of , , , , , .

public static int getAlphabeticModifiers(MenuItem item)

Return the modifier for this menu item's alphabetic shortcut. The modifier is a combination of , , , , , . For example, |

Returns:

Modifier associated with the keyboard shortcut.

public static 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 by default.

Subsequent calls to MenuItem or MenuItem will automatically mutate the icon and apply the specified tint and tint mode.

Parameters:

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

See also: MenuItemCompat.getIconTintList(MenuItem)

public static ColorStateList getIconTintList(MenuItem item)

Returns:

the tint applied to the item's icon

See also: MenuItemCompat.setIconTintList(MenuItem, ColorStateList)

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

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

Parameters:

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

See also: MenuItemCompat.setIconTintList(MenuItem, ColorStateList)

public static PorterDuff.Mode getIconTintMode(MenuItem item)

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

Returns:

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

See also: MenuItemCompat.setIconTintMode(MenuItem, PorterDuff.Mode)

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.core.view;

import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.DoNotInline;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.internal.view.SupportMenuItem;

/**
 * Helper for accessing features in {@link MenuItem}.
 * <p class="note"><strong>Note:</strong> You cannot get an instance of this class. Instead,
 * it provides <em>static</em> methods that correspond to the methods in {@link
 * MenuItem}, but take a {@link MenuItem} object as an additional
 * argument.</p>
 */
@SuppressWarnings("deprecation")
public final class MenuItemCompat {
    private static final String TAG = "MenuItemCompat";

    /**
     * Never show this item as a button in an Action Bar.
     *
     * @deprecated Use {@link MenuItem#SHOW_AS_ACTION_NEVER} directly.
     */
    @Deprecated
    public static final int SHOW_AS_ACTION_NEVER = 0;

    /**
     * Show this item as a button in an Action Bar if the system
     * decides there is room for it.
     *
     * @deprecated Use {@link MenuItem#SHOW_AS_ACTION_IF_ROOM} directly.
     */
    @Deprecated
    public static final int SHOW_AS_ACTION_IF_ROOM = 1;

    /**
     * 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.
     *
     * @deprecated Use {@link MenuItem#SHOW_AS_ACTION_ALWAYS} directly.
     */
    @Deprecated
    public static final int SHOW_AS_ACTION_ALWAYS = 2;

    /**
     * When this item is in the action bar, always show it with a
     * text label even if it also has an icon specified.
     *
     * @deprecated Use {@link MenuItem#SHOW_AS_ACTION_WITH_TEXT} directly.
     */
    @Deprecated
    public static final int SHOW_AS_ACTION_WITH_TEXT = 4;

    /**
     * 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.
     *
     * @deprecated Use {@link MenuItem#SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW} directly.
     */
    @Deprecated
    public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8;

    /**
     * Interface definition for a callback to be invoked when a menu item marked with {@link
     * #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW} is expanded or collapsed.
     *
     * @see #expandActionView(MenuItem)
     * @see #collapseActionView(MenuItem)
     * @see #setShowAsAction(MenuItem, int)
     *
     * @deprecated Use {@link MenuItem.OnActionExpandListener} directly.
     */
    @Deprecated
    public interface OnActionExpandListener {

        /**
         * Called when a menu item with {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}
         * is expanded.
         *
         * @param item Item that was expanded
         * @return true if the item should expand, false if expansion should be suppressed.
         */
        boolean onMenuItemActionExpand(MenuItem item);

        /**
         * Called when a menu item with {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}
         * is collapsed.
         *
         * @param item Item that was collapsed
         * @return true if the item should collapse, false if collapsing should be suppressed.
         */
        boolean onMenuItemActionCollapse(MenuItem item);
    }

    // -------------------------------------------------------------------

    /**
     * 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
     * {@link MenuItem#setShowAsAction(int)}.
     *
     * @param item - the item to change
     * @param actionEnum - How the item should display.
     *
     * @deprecated Use {@link MenuItem#setShowAsAction(int)} directly.
     */
    @Deprecated
    public static void setShowAsAction(MenuItem item, int actionEnum) {
        item.setShowAsAction(actionEnum);
    }

    /**
     * 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.
     *
     * @param item the item to change
     * @param view View to use for presenting this item to the user.
     * @return This Item so additional setters can be called.
     *
     * @see #setShowAsAction(MenuItem, int)
     *
     * @deprecated Use {@link MenuItem#setActionView(View)} directly.
     */
    @Deprecated
    public static MenuItem setActionView(MenuItem item, View view) {
        return item.setActionView(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.
     * <p>
     *   <strong>Note:</strong> Setting an action view overrides the action provider
     *           set via {@link #setActionProvider(MenuItem, ActionProvider)}.
     * </p>
     *
     * @param item the item to change
     * @param resId Layout resource to use for presenting this item to the user.
     * @return This Item so additional setters can be called.
     *
     * @see #setShowAsAction(MenuItem, int)
     *
     * @deprecated Use {@link MenuItem#setActionView(int)} directly.
     */
    @Deprecated
    public static MenuItem setActionView(MenuItem item, int resId) {
        return item.setActionView(resId);
    }

    /**
     * Returns the currently set action view for this menu item.
     *
     * @param item the item to query
     * @return This item's action view
     *
     * @deprecated Use {@link MenuItem#getActionView()} directly.
     */
    @Deprecated
    public static View getActionView(MenuItem item) {
        return item.getActionView();
    }

    /**
     * Sets the {@link 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.
     * <p>
     *   <strong>Note:</strong> Setting an action provider overrides the action view
     *           set via {@link #setActionView(MenuItem, View)}.
     * </p>
     *
     * @param item item to change
     * @param provider The action provider.
     * @return This Item so additional setters can be called.
     *
     * @see ActionProvider
     */
    @Nullable
    public static MenuItem setActionProvider(@NonNull MenuItem item,
            @Nullable ActionProvider provider) {
        if (item instanceof SupportMenuItem) {
            return ((SupportMenuItem) item).setSupportActionProvider(provider);
        }
        // TODO Wrap the support ActionProvider and assign it
        Log.w(TAG, "setActionProvider: item does not implement SupportMenuItem; ignoring");
        return item;
    }

    /**
     * Gets the {@link ActionProvider}.
     *
     * @return The action provider.
     *
     * @see ActionProvider
     * @see #setActionProvider(MenuItem, ActionProvider)
     */
    @Nullable
    public static ActionProvider getActionProvider(@NonNull MenuItem item) {
        if (item instanceof SupportMenuItem) {
            return ((SupportMenuItem) item).getSupportActionProvider();
        }

        // TODO Wrap the framework ActionProvider and return it
        Log.w(TAG, "getActionProvider: item does not implement SupportMenuItem; returning null");
        return null;
    }

    /**
     * Expand the action view associated with this menu item.
     * The menu item must have an action view set, as well as
     * the showAsAction flag {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}.
     * If a listener has been set using
     * {@link #setOnActionExpandListener(MenuItem, OnActionExpandListener)}
     * it will have its {@link OnActionExpandListener#onMenuItemActionExpand(MenuItem)}
     * method invoked. The listener may return false from this method to prevent expanding
     * the action view.
     *
     * @return true if the action view was expanded, false otherwise.
     *
     * @deprecated Use {@link MenuItem#expandActionView()} directly.
     */
    @Deprecated
    public static boolean expandActionView(MenuItem item) {
        return item.expandActionView();
    }

    /**
     * Collapse the action view associated with this menu item. The menu item must have an action
     * view set, as well as the showAsAction flag {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}. If a
     * listener has been set using {@link #setOnActionExpandListener(MenuItem,
     * OnActionExpandListener)}
     * it will have its {@link
     * OnActionExpandListener#onMenuItemActionCollapse(MenuItem)}
     * method invoked. The listener may return false from this method to prevent collapsing
     * the action view.
     *
     * @return true if the action view was collapsed, false otherwise.
     *
     * @deprecated Use {@link MenuItem#collapseActionView()} directly.
     */
    @Deprecated
    public static boolean collapseActionView(MenuItem item) {
        return item.collapseActionView();
    }

    /**
     * Returns true if this menu item's action view has been expanded.
     *
     * @return true if the item's action view is expanded, false otherwise.
     * @see #expandActionView(MenuItem)
     * @see #collapseActionView(MenuItem)
     * @see #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
     * @see OnActionExpandListener
     *
     * @deprecated Use {@link MenuItem#isActionViewExpanded()} directly.
     */
    @Deprecated
    public static boolean isActionViewExpanded(MenuItem item) {
        return item.isActionViewExpanded();
    }

    /**
     * Set an {@link 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
     * {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}.
     *
     * @param listener Listener that will respond to expand/collapse events
     * @return This menu item instance for call chaining
     *
     * @deprecated Use {@link MenuItem#setOnActionExpandListener(MenuItem.OnActionExpandListener)}
     * directly.
     */
    @Deprecated
    public static MenuItem setOnActionExpandListener(MenuItem item,
            final OnActionExpandListener listener) {
        return item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return listener.onMenuItemActionExpand(item);
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                return listener.onMenuItemActionCollapse(item);
            }
        });
    }

    /**
     * Change the content description associated with this menu item.
     *
     * @param item item to change.
     * @param contentDescription The new content description.
     */
    public static void setContentDescription(@NonNull MenuItem item,
            @Nullable CharSequence contentDescription) {
        if (item instanceof SupportMenuItem) {
            ((SupportMenuItem) item).setContentDescription(contentDescription);
        } else if (Build.VERSION.SDK_INT >= 26) {
            Api26Impl.setContentDescription(item, contentDescription);
        }
    }

    /**
     * Retrieve the content description associated with this menu item.
     *
     * @return The content description.
     */
    @Nullable
    @SuppressWarnings("RedundantCast")
    public static CharSequence getContentDescription(@NonNull MenuItem item) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            return ((SupportMenuItem) item).getContentDescription();
        }
        if (Build.VERSION.SDK_INT >= 26) {
            return Api26Impl.getContentDescription(item);
        }
        return null;
    }

    /**
     * Change the tooltip text associated with this menu item.
     *
     * @param item item to change.
     * @param tooltipText The new tooltip text
     */
    public static void setTooltipText(@NonNull MenuItem item, @Nullable CharSequence tooltipText) {
        if (item instanceof SupportMenuItem) {
            ((SupportMenuItem) item).setTooltipText(tooltipText);
        } else if (Build.VERSION.SDK_INT >= 26) {
            Api26Impl.setTooltipText(item, tooltipText);
        }
    }

    /**
     * Retrieve the tooltip text associated with this menu item.
     *
     * @return The tooltip text.
     */
    @Nullable
    @SuppressWarnings("RedundantCast")
    public static CharSequence getTooltipText(@NonNull MenuItem item) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            return ((SupportMenuItem) item).getTooltipText();
        }
        if (Build.VERSION.SDK_INT >= 26) {
            return Api26Impl.getTooltipText(item);
        }
        return null;
    }

    /**
     * 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.
     * <p>
     * See {@link Menu} for the menu types that support shortcuts.
     *
     * @param numericChar The numeric shortcut key. This is the shortcut when
     *        using a numeric (e.g., 12-key) keyboard.
     * @param numericModifiers The numeric modifier associated with the shortcut. It should
     *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
     *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
     *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
     * @param alphaChar The alphabetic shortcut key. This is the shortcut when
     *        using a keyboard with alphabetic keys.
     * @param alphaModifiers The alphabetic modifier associated with the shortcut. It should
     *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
     *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
     *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
     */
    @SuppressWarnings("RedundantCast")
    public static void setShortcut(@NonNull MenuItem item, char numericChar, char alphaChar,
            int numericModifiers, int alphaModifiers) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            ((SupportMenuItem) item).setShortcut(numericChar, alphaChar, numericModifiers,
                    alphaModifiers);
        } else if (Build.VERSION.SDK_INT >= 26) {
            Api26Impl.setShortcut(item, numericChar, alphaChar, numericModifiers, alphaModifiers);
        }
    }

    /**
     * Change the numeric shortcut and modifiers associated with this item.
     * <p>
     * See {@link Menu} for the menu types that support shortcuts.
     *
     * @param numericChar The numeric shortcut key.  This is the shortcut when
     *                 using a 12-key (numeric) keyboard.
     * @param numericModifiers The modifier associated with the shortcut. It should
     *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
     *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
     *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
     */
    @SuppressWarnings("RedundantCast")
    public static void setNumericShortcut(@NonNull MenuItem item, char numericChar,
            int numericModifiers) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            ((SupportMenuItem) item).setNumericShortcut(numericChar, numericModifiers);
        } else if (Build.VERSION.SDK_INT >= 26) {
            Api26Impl.setNumericShortcut(item, numericChar, numericModifiers);
        }
    }

    /**
     * Return the modifiers for this menu item's numeric (12-key) shortcut.
     * The modifier is a combination of {@link KeyEvent#META_META_ON},
     * {@link KeyEvent#META_CTRL_ON}, {@link KeyEvent#META_ALT_ON},
     * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_SYM_ON},
     * {@link KeyEvent#META_FUNCTION_ON}.
     * For example, {@link KeyEvent#META_FUNCTION_ON}|{@link KeyEvent#META_CTRL_ON}
     *
     * @return Modifier associated with the numeric shortcut.
     */
    @SuppressWarnings("RedundantCast")
    public static int getNumericModifiers(@NonNull MenuItem item) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            return ((SupportMenuItem) item).getNumericModifiers();
        }
        if (Build.VERSION.SDK_INT >= 26) {
            return Api26Impl.getNumericModifiers(item);
        }
        return 0;
    }

    /**
     * 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.
     * <p>
     * See {@link Menu} for the menu types that support shortcuts.
     *
     * @param alphaChar The alphabetic shortcut key. This is the shortcut when
     *        using a keyboard with alphabetic keys.
     * @param alphaModifiers The modifier associated with the shortcut. It should
     *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
     *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
     *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
     */
    @SuppressWarnings("RedundantCast")
    public static void setAlphabeticShortcut(@NonNull MenuItem item, char alphaChar,
            int alphaModifiers) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            ((SupportMenuItem) item).setAlphabeticShortcut(alphaChar, alphaModifiers);
        } else if (Build.VERSION.SDK_INT >= 26) {
            Api26Impl.setAlphabeticShortcut(item, alphaChar, alphaModifiers);
        }
    }

    /**
     * Return the modifier for this menu item's alphabetic shortcut.
     * The modifier is a combination of {@link KeyEvent#META_META_ON},
     * {@link KeyEvent#META_CTRL_ON}, {@link KeyEvent#META_ALT_ON},
     * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_SYM_ON},
     * {@link KeyEvent#META_FUNCTION_ON}.
     * For example, {@link KeyEvent#META_FUNCTION_ON}|{@link KeyEvent#META_CTRL_ON}
     *
     * @return Modifier associated with the keyboard shortcut.
     */
    @SuppressWarnings("RedundantCast")
    public static int getAlphabeticModifiers(@NonNull MenuItem item) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            return ((SupportMenuItem) item).getAlphabeticModifiers();
        }
        if (Build.VERSION.SDK_INT >= 26) {
            return Api26Impl.getAlphabeticModifiers(item);
        }
        return 0;
    }

    /**
     * Applies a tint to the item's icon. Does not modify the
     * current tint mode of that item, which is {@link PorterDuff.Mode#SRC_IN} by default.
     * <p>
     * Subsequent calls to {@link MenuItem#setIcon(Drawable)} or {@link MenuItem#setIcon(int)} will
     * automatically mutate the icon and apply the specified tint and
     * tint mode.
     *
     * @param tint the tint to apply, may be {@code null} to clear tint
     *
     * @see #getIconTintList(MenuItem)
     */
    @SuppressWarnings("RedundantCast")
    public static void setIconTintList(@NonNull MenuItem item, @Nullable ColorStateList tint) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            ((SupportMenuItem) item).setIconTintList(tint);
        } else if (Build.VERSION.SDK_INT >= 26) {
            Api26Impl.setIconTintList(item, tint);
        }
    }

    /**
     * @return the tint applied to the item's icon
     * @see #setIconTintList(MenuItem, ColorStateList)
     */
    @Nullable
    @SuppressWarnings("RedundantCast")
    public static ColorStateList getIconTintList(@NonNull MenuItem item) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            return ((SupportMenuItem) item).getIconTintList();
        }
        if (Build.VERSION.SDK_INT >= 26) {
            return Api26Impl.getIconTintList(item);
        }
        return null;
    }

    /**
     * Specifies the blending mode used to apply the tint specified by
     * {@link #setIconTintList(MenuItem, ColorStateList)} to the item's icon. The default mode is
     * {@link PorterDuff.Mode#SRC_IN}.
     *
     * @param tintMode the blending mode used to apply the tint, may be
     *                 {@code null} to clear tint
     * @see #setIconTintList(MenuItem, ColorStateList)
     */
    @SuppressWarnings("RedundantCast")
    public static void setIconTintMode(@NonNull MenuItem item, @Nullable PorterDuff.Mode tintMode) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            ((SupportMenuItem) item).setIconTintMode(tintMode);
        } else if (Build.VERSION.SDK_INT >= 26) {
            Api26Impl.setIconTintMode(item, tintMode);
        }
    }

    /**
     * Returns the blending mode used to apply the tint to the item's icon, if specified.
     *
     * @return the blending mode used to apply the tint to the item's icon
     * @see #setIconTintMode(MenuItem, PorterDuff.Mode)
     */
    @Nullable
    @SuppressWarnings("RedundantCast")
    public static PorterDuff.Mode getIconTintMode(@NonNull MenuItem item) {
        if (item instanceof SupportMenuItem) {
            // Cast required to target SupportMenuItem method declaration.
            return ((SupportMenuItem) item).getIconTintMode();
        }
        if (Build.VERSION.SDK_INT >= 26) {
            return Api26Impl.getIconTintMode(item);
        }
        return null;
    }

    private MenuItemCompat() {}

    @RequiresApi(26)
    static class Api26Impl {
        private Api26Impl() {
            // This class is not instantiable.
        }

        @DoNotInline
        static MenuItem setContentDescription(MenuItem menuItem, CharSequence contentDescription) {
            return menuItem.setContentDescription(contentDescription);
        }

        @DoNotInline
        static CharSequence getContentDescription(MenuItem menuItem) {
            return menuItem.getContentDescription();
        }

        @DoNotInline
        static MenuItem setTooltipText(MenuItem menuItem, CharSequence tooltipText) {
            return menuItem.setTooltipText(tooltipText);
        }

        @DoNotInline
        static CharSequence getTooltipText(MenuItem menuItem) {
            return menuItem.getTooltipText();
        }

        @DoNotInline
        static MenuItem setShortcut(MenuItem menuItem, char numericChar, char alphaChar,
                int numericModifiers, int alphaModifiers) {
            return menuItem.setShortcut(numericChar, alphaChar, numericModifiers, alphaModifiers);
        }

        @DoNotInline
        static MenuItem setNumericShortcut(MenuItem menuItem, char numericChar,
                int numericModifiers) {
            return menuItem.setNumericShortcut(numericChar, numericModifiers);
        }

        @DoNotInline
        static int getNumericModifiers(MenuItem menuItem) {
            return menuItem.getNumericModifiers();
        }

        @DoNotInline
        static MenuItem setAlphabeticShortcut(MenuItem menuItem, char alphaChar,
                int alphaModifiers) {
            return menuItem.setAlphabeticShortcut(alphaChar, alphaModifiers);
        }

        @DoNotInline
        static int getAlphabeticModifiers(MenuItem menuItem) {
            return menuItem.getAlphabeticModifiers();
        }

        @DoNotInline
        static MenuItem setIconTintList(MenuItem menuItem, ColorStateList tint) {
            return menuItem.setIconTintList(tint);
        }

        @DoNotInline
        static ColorStateList getIconTintList(MenuItem menuItem) {
            return menuItem.getIconTintList();
        }

        @DoNotInline
        static MenuItem setIconTintMode(MenuItem menuItem, PorterDuff.Mode tintMode) {
            return menuItem.setIconTintMode(tintMode);
        }

        @DoNotInline
        static PorterDuff.Mode getIconTintMode(MenuItem menuItem) {
            return menuItem.getIconTintMode();
        }
    }
}