public class

ActionMenuItem

extends java.lang.Object

implements SupportMenuItem

 java.lang.Object

↳androidx.appcompat.view.menu.ActionMenuItem

Gradle dependencies

compile group: 'androidx.appcompat', name: 'appcompat', version: '1.7.0'

  • groupId: androidx.appcompat
  • artifactId: appcompat
  • version: 1.7.0

Artifact androidx.appcompat:appcompat:1.7.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.appcompat:appcompat com.android.support:appcompat-v7

Androidx class mapping:

androidx.appcompat.view.menu.ActionMenuItem android.support.v7.view.menu.ActionMenuItem

Summary

Constructors
publicActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering, java.lang.CharSequence title)

Methods
public booleancollapseActionView()

public booleanexpandActionView()

public android.view.ActionProvidergetActionProvider()

public ViewgetActionView()

public intgetAlphabeticModifiers()

public chargetAlphabeticShortcut()

public java.lang.CharSequencegetContentDescription()

public intgetGroupId()

public DrawablegetIcon()

public ColorStateListgetIconTintList()

public PorterDuff.ModegetIconTintMode()

public IntentgetIntent()

public intgetItemId()

public ContextMenuInfogetMenuInfo()

public intgetNumericModifiers()

public chargetNumericShortcut()

public intgetOrder()

public SubMenugetSubMenu()

public ActionProvidergetSupportActionProvider()

public java.lang.CharSequencegetTitle()

public java.lang.CharSequencegetTitleCondensed()

public java.lang.CharSequencegetTooltipText()

public booleanhasSubMenu()

public booleaninvoke()

public booleanisActionViewExpanded()

public booleanisCheckable()

public booleanisChecked()

public booleanisEnabled()

public booleanisVisible()

public booleanrequiresActionButton()

public booleanrequiresOverflow()

public MenuItemsetActionProvider(android.view.ActionProvider actionProvider)

public SupportMenuItemsetActionView(View actionView)

public MenuItemsetAlphabeticShortcut(char alphaChar)

public MenuItemsetAlphabeticShortcut(char alphaChar, int alphaModifiers)

public MenuItemsetCheckable(boolean checkable)

public MenuItemsetChecked(boolean checked)

public SupportMenuItemsetContentDescription(java.lang.CharSequence contentDescription)

public MenuItemsetEnabled(boolean enabled)

public ActionMenuItemsetExclusiveCheckable(boolean exclusive)

public MenuItemsetIcon(Drawable icon)

public MenuItemsetIcon(int iconRes)

public MenuItemsetIconTintList(ColorStateList iconTintList)

public MenuItemsetIconTintMode(PorterDuff.Mode iconTintMode)

public MenuItemsetIntent(Intent intent)

public MenuItemsetNumericShortcut(char numericChar)

public MenuItemsetNumericShortcut(char numericChar, int numericModifiers)

public MenuItemsetOnActionExpandListener(MenuItem.OnActionExpandListener listener)

public MenuItemsetOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener)

public MenuItemsetShortcut(char numericChar, char alphaChar)

public MenuItemsetShortcut(char numericChar, char alphaChar, int numericModifiers, int alphaModifiers)

public voidsetShowAsAction(int show)

public SupportMenuItemsetShowAsActionFlags(int actionEnum)

public SupportMenuItemsetSupportActionProvider(ActionProvider actionProvider)

public MenuItemsetTitle(java.lang.CharSequence title)

public MenuItemsetTitle(int title)

public MenuItemsetTitleCondensed(java.lang.CharSequence title)

public SupportMenuItemsetTooltipText(java.lang.CharSequence tooltipText)

public MenuItemsetVisible(boolean visible)

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

Constructors

public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering, java.lang.CharSequence title)

Methods

public char getAlphabeticShortcut()

public int getAlphabeticModifiers()

public int getGroupId()

public Drawable getIcon()

public Intent getIntent()

public int getItemId()

public ContextMenuInfo getMenuInfo()

public char getNumericShortcut()

public int getNumericModifiers()

public int getOrder()

public SubMenu getSubMenu()

public java.lang.CharSequence getTitle()

public java.lang.CharSequence getTitleCondensed()

public boolean hasSubMenu()

public boolean isCheckable()

public boolean isChecked()

public boolean isEnabled()

public boolean isVisible()

public MenuItem setAlphabeticShortcut(char alphaChar)

public MenuItem setAlphabeticShortcut(char alphaChar, int alphaModifiers)

public MenuItem setCheckable(boolean checkable)

public ActionMenuItem setExclusiveCheckable(boolean exclusive)

public MenuItem setChecked(boolean checked)

public MenuItem setEnabled(boolean enabled)

public MenuItem setIcon(Drawable icon)

public MenuItem setIcon(int iconRes)

public MenuItem setIntent(Intent intent)

public MenuItem setNumericShortcut(char numericChar)

public MenuItem setNumericShortcut(char numericChar, int numericModifiers)

public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener)

public MenuItem setShortcut(char numericChar, char alphaChar)

public MenuItem setShortcut(char numericChar, char alphaChar, int numericModifiers, int alphaModifiers)

public MenuItem setTitle(java.lang.CharSequence title)

public MenuItem setTitle(int title)

public MenuItem setTitleCondensed(java.lang.CharSequence title)

public MenuItem setVisible(boolean visible)

public boolean invoke()

public void setShowAsAction(int show)

public boolean requiresActionButton()

public boolean requiresOverflow()

public SupportMenuItem setActionView(View actionView)

public View getActionView()

public MenuItem setActionProvider(android.view.ActionProvider actionProvider)

public android.view.ActionProvider getActionProvider()

public ActionProvider getSupportActionProvider()

public SupportMenuItem setSupportActionProvider(ActionProvider actionProvider)

public SupportMenuItem setShowAsActionFlags(int actionEnum)

public boolean expandActionView()

public boolean collapseActionView()

public boolean isActionViewExpanded()

public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener)

public SupportMenuItem setContentDescription(java.lang.CharSequence contentDescription)

public java.lang.CharSequence getContentDescription()

public SupportMenuItem setTooltipText(java.lang.CharSequence tooltipText)

public java.lang.CharSequence getTooltipText()

public MenuItem setIconTintList(ColorStateList iconTintList)

public ColorStateList getIconTintList()

public MenuItem setIconTintMode(PorterDuff.Mode iconTintMode)

public PorterDuff.Mode getIconTintMode()

Source

/*
 * Copyright (C) 2010 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.appcompat.view.menu;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.internal.view.SupportMenuItem;
import androidx.core.view.ActionProvider;

/**
 */
@RestrictTo(LIBRARY_GROUP_PREFIX)
public class ActionMenuItem implements SupportMenuItem {

    private final int mId;
    private final int mGroup;
    private final int mOrdering;

    private CharSequence mTitle;
    private CharSequence mTitleCondensed;
    private Intent mIntent;
    private char mShortcutNumericChar;
    private int mShortcutNumericModifiers = KeyEvent.META_CTRL_ON;
    private char mShortcutAlphabeticChar;
    private int mShortcutAlphabeticModifiers = KeyEvent.META_CTRL_ON;

    private Drawable mIconDrawable;

    private Context mContext;

    private SupportMenuItem.OnMenuItemClickListener mClickListener;

    private CharSequence mContentDescription;
    private CharSequence mTooltipText;

    private ColorStateList mIconTintList = null;
    private PorterDuff.Mode mIconTintMode = null;
    private boolean mHasIconTint = false;
    private boolean mHasIconTintMode = false;

    private int mFlags = ENABLED;
    private static final int CHECKABLE = 0x00000001;
    private static final int CHECKED = 0x00000002;
    private static final int EXCLUSIVE = 0x00000004;
    private static final int HIDDEN = 0x00000008;
    private static final int ENABLED = 0x00000010;

    public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering,
            CharSequence title) {
        mContext = context;
        mId = id;
        mGroup = group;
        mOrdering = ordering;
        mTitle = title;
    }

    @Override
    public char getAlphabeticShortcut() {
        return mShortcutAlphabeticChar;
    }

    @Override
    public int getAlphabeticModifiers() {
        return mShortcutAlphabeticModifiers;
    }

    @Override
    public int getGroupId() {
        return mGroup;
    }

    @Override
    public Drawable getIcon() {
        return mIconDrawable;
    }

    @Override
    public Intent getIntent() {
        return mIntent;
    }

    @Override
    public int getItemId() {
        return mId;
    }

    @Override
    public ContextMenuInfo getMenuInfo() {
        return null;
    }

    @Override
    public char getNumericShortcut() {
        return mShortcutNumericChar;
    }

    @Override
    public int getNumericModifiers() {
        return mShortcutNumericModifiers;
    }

    @Override
    public int getOrder() {
        return mOrdering;
    }

    @Override
    public SubMenu getSubMenu() {
        return null;
    }

    @Override
    public CharSequence getTitle() {
        return mTitle;
    }

    @Override
    public CharSequence getTitleCondensed() {
        return mTitleCondensed != null ? mTitleCondensed : mTitle;
    }

    @Override
    public boolean hasSubMenu() {
        return false;
    }

    @Override
    public boolean isCheckable() {
        return (mFlags & CHECKABLE) != 0;
    }

    @Override
    public boolean isChecked() {
        return (mFlags & CHECKED) != 0;
    }

    @Override
    public boolean isEnabled() {
        return (mFlags & ENABLED) != 0;
    }

    @Override
    public boolean isVisible() {
        return (mFlags & HIDDEN) == 0;
    }

    @Override
    public MenuItem setAlphabeticShortcut(char alphaChar) {
        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
        return this;
    }

    @NonNull
    @Override
    public MenuItem setAlphabeticShortcut(char alphaChar, int alphaModifiers) {
        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
        mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers);
        return this;
    }

    @Override
    public MenuItem setCheckable(boolean checkable) {
        mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
        return this;
    }

    public ActionMenuItem setExclusiveCheckable(boolean exclusive) {
        mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
        return this;
    }

    @Override
    public MenuItem setChecked(boolean checked) {
        mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
        return this;
    }

    @Override
    public MenuItem setEnabled(boolean enabled) {
        mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0);
        return this;
    }

    @Override
    public MenuItem setIcon(Drawable icon) {
        mIconDrawable = icon;

        applyIconTint();
        return this;
    }

    @Override
    public MenuItem setIcon(int iconRes) {
        mIconDrawable = ContextCompat.getDrawable(mContext, iconRes);

        applyIconTint();
        return this;
    }

    @Override
    public MenuItem setIntent(Intent intent) {
        mIntent = intent;
        return this;
    }

    @Override
    public MenuItem setNumericShortcut(char numericChar) {
        mShortcutNumericChar = numericChar;
        return this;
    }

    @NonNull
    @Override
    public MenuItem setNumericShortcut(char numericChar, int numericModifiers) {
        mShortcutNumericChar = numericChar;
        mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers);
        return this;
    }

    @Override
    public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
        mClickListener = menuItemClickListener;
        return this;
    }

    @Override
    public MenuItem setShortcut(char numericChar, char alphaChar) {
        mShortcutNumericChar = numericChar;
        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
        return this;
    }

    @NonNull
    @Override
    public MenuItem setShortcut(char numericChar, char alphaChar, int numericModifiers,
            int alphaModifiers) {
        mShortcutNumericChar = numericChar;
        mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers);
        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
        mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers);
        return this;
    }

    @Override
    public MenuItem setTitle(CharSequence title) {
        mTitle = title;
        return this;
    }

    @Override
    public MenuItem setTitle(int title) {
        mTitle = mContext.getResources().getString(title);
        return this;
    }

    @Override
    public MenuItem setTitleCondensed(CharSequence title) {
        mTitleCondensed = title;
        return this;
    }

    @Override
    public MenuItem setVisible(boolean visible) {
        mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN);
        return this;
    }

    public boolean invoke() {
        if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
            return true;
        }

        if (mIntent != null) {
            mContext.startActivity(mIntent);
            return true;
        }

        return false;
    }

    @Override
    public void setShowAsAction(int show) {
        // Do nothing. ActionMenuItems always show as action buttons.
    }

    @Override
    public boolean requiresActionButton() {
        return true;
    }

    @Override
    public boolean requiresOverflow() {
        return false;
    }

    @NonNull
    @Override
    public SupportMenuItem setActionView(View actionView) {
        throw new UnsupportedOperationException();
    }

    @Override
    public View getActionView() {
        return null;
    }

    @Override
    public MenuItem setActionProvider(android.view.ActionProvider actionProvider) {
        throw new UnsupportedOperationException();
    }

    @Override
    public android.view.ActionProvider getActionProvider() {
        throw new UnsupportedOperationException();
    }

    @NonNull
    @Override
    public SupportMenuItem setActionView(int resId) {
        throw new UnsupportedOperationException();
    }

    @Override
    public ActionProvider getSupportActionProvider() {
        return null;
    }

    @NonNull
    @Override
    public SupportMenuItem setSupportActionProvider(ActionProvider actionProvider) {
        throw new UnsupportedOperationException();
    }

    @NonNull
    @Override
    public SupportMenuItem setShowAsActionFlags(int actionEnum) {
        setShowAsAction(actionEnum);
        return this;
    }

    @Override
    public boolean expandActionView() {
        return false;
    }

    @Override
    public boolean collapseActionView() {
        return false;
    }

    @Override
    public boolean isActionViewExpanded() {
        return false;
    }

    @Override
    public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener) {
        throw new UnsupportedOperationException();
    }

    @NonNull
    @Override
    public SupportMenuItem setContentDescription(CharSequence contentDescription) {
        mContentDescription = contentDescription;
        return this;
    }

    @Override
    public CharSequence getContentDescription() {
        return mContentDescription;
    }

    @NonNull
    @Override
    public SupportMenuItem setTooltipText(CharSequence tooltipText) {
        mTooltipText = tooltipText;
        return this;
    }

    @Override
    public CharSequence getTooltipText() {
        return mTooltipText;
    }

    @NonNull
    @Override
    public MenuItem setIconTintList(@Nullable ColorStateList iconTintList) {
        mIconTintList = iconTintList;
        mHasIconTint = true;

        applyIconTint();

        return this;
    }

    @Override
    public ColorStateList getIconTintList() {
        return mIconTintList;
    }

    @NonNull
    @Override
    public MenuItem setIconTintMode(PorterDuff.Mode iconTintMode) {
        mIconTintMode = iconTintMode;
        mHasIconTintMode = true;

        applyIconTint();

        return this;
    }

    @Override
    public PorterDuff.Mode getIconTintMode() {
        return mIconTintMode;
    }

    private void applyIconTint() {
        if (mIconDrawable != null && (mHasIconTint || mHasIconTintMode)) {
            mIconDrawable = DrawableCompat.wrap(mIconDrawable);
            mIconDrawable = mIconDrawable.mutate();

            if (mHasIconTint) {
                DrawableCompat.setTintList(mIconDrawable, mIconTintList);
            }

            if (mHasIconTintMode) {
                DrawableCompat.setTintMode(mIconDrawable, mIconTintMode);
            }
        }
    }
}