FileDocCategorySizeDatePackage
IconMenuItemView.javaAPI DocAndroid 5.1 API10226Thu Mar 12 22:22:10 GMT 2015com.android.internal.view.menu

IconMenuItemView

public final class IconMenuItemView extends android.widget.TextView implements MenuView.ItemView
The item view for each item in the {@link IconMenuView}.

Fields Summary
private static final int
NO_ALPHA
private IconMenuView
mIconMenuView
private com.android.internal.view.menu.MenuBuilder.ItemInvoker
mItemInvoker
private MenuItemImpl
mItemData
private android.graphics.drawable.Drawable
mIcon
private int
mTextAppearance
private android.content.Context
mTextAppearanceContext
private float
mDisabledAlpha
private android.graphics.Rect
mPositionIconAvailable
private android.graphics.Rect
mPositionIconOutput
private boolean
mShortcutCaptionMode
private String
mShortcutCaption
private static String
sPrependShortcutLabel
Constructors Summary
public IconMenuItemView(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)


             
        super(context, attrs, defStyleAttr, defStyleRes);

        if (sPrependShortcutLabel == null) {
            /*
             * Views should only be constructed from the UI thread, so no
             * synchronization needed
             */
            sPrependShortcutLabel = getResources().getString(
                    com.android.internal.R.string.prepend_shortcut_label);
        }

        final TypedArray a = context.obtainStyledAttributes(
                attrs, com.android.internal.R.styleable.MenuView, defStyleAttr, defStyleRes);

        mDisabledAlpha = a.getFloat(
                com.android.internal.R.styleable.MenuView_itemIconDisabledAlpha, 0.8f);
        mTextAppearance = a.getResourceId(com.android.internal.R.styleable.
                                          MenuView_itemTextAppearance, -1);
        mTextAppearanceContext = context;
        
        a.recycle();
    
public IconMenuItemView(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)

        this(context, attrs, defStyleAttr, 0);
    
public IconMenuItemView(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, 0);
    
Methods Summary
protected voiddrawableStateChanged()

        super.drawableStateChanged();

        if (mItemData != null && mIcon != null) {
            // When disabled, the not-focused state and the pressed state should
            // drop alpha on the icon
            final boolean isInAlphaState = !mItemData.isEnabled() && (isPressed() || !isFocused());
            mIcon.setAlpha(isInAlphaState ? (int) (mDisabledAlpha * NO_ALPHA) : NO_ALPHA);
        }
    
public MenuItemImplgetItemData()

        return mItemData;
    
IconMenuView.LayoutParamsgetTextAppropriateLayoutParams()

return
layout params appropriate for this view. If layout params already exist, it will augment them to be appropriate to the current text size.

        IconMenuView.LayoutParams lp = (IconMenuView.LayoutParams) getLayoutParams();
        if (lp == null) {
            // Default layout parameters
            lp = new IconMenuView.LayoutParams(
                    IconMenuView.LayoutParams.MATCH_PARENT, IconMenuView.LayoutParams.MATCH_PARENT);
        }

        // Set the desired width of item
        lp.desiredWidth = (int) Layout.getDesiredWidth(getText(), getPaint());

        return lp;
    
voidinitialize(java.lang.CharSequence title, android.graphics.drawable.Drawable icon)
Initializes with the provided title and icon

param
title The title of this item
param
icon The icon of this item

        setClickable(true);
        setFocusable(true);

        if (mTextAppearance != -1) {
            setTextAppearance(mTextAppearanceContext, mTextAppearance);
        }

        setTitle(title);
        setIcon(icon);
    
public voidinitialize(MenuItemImpl itemData, int menuType)

        mItemData = itemData;

        initialize(itemData.getTitleForItemView(this), itemData.getIcon());
        
        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
        setEnabled(itemData.isEnabled());
    
protected voidonLayout(boolean changed, int left, int top, int right, int bottom)

        super.onLayout(changed, left, top, right, bottom);
        
        positionIcon();
    
protected voidonTextChanged(java.lang.CharSequence text, int start, int before, int after)

        super.onTextChanged(text, start, before, after);

        // our layout params depend on the length of the text
        setLayoutParams(getTextAppropriateLayoutParams());
    
public booleanperformClick()

        // Let the view's click listener have top priority (the More button relies on this)
        if (super.performClick()) {
            return true;
        }
        
        if ((mItemInvoker != null) && (mItemInvoker.invokeItem(mItemData))) {
            playSoundEffect(SoundEffectConstants.CLICK);
            return true;
        } else {
            return false;
        }
    
private voidpositionIcon()
Positions the icon vertically (horizontal centering is taken care of by the TextView's gravity).

        
        if (mIcon == null) {
            return;
        }
        
        // We reuse the output rectangle as a temp rect
        Rect tmpRect = mPositionIconOutput;
        getLineBounds(0, tmpRect);
        mPositionIconAvailable.set(0, 0, getWidth(), tmpRect.top);
        final int layoutDirection = getLayoutDirection();
        Gravity.apply(Gravity.CENTER_VERTICAL | Gravity.START, mIcon.getIntrinsicWidth(), mIcon
                .getIntrinsicHeight(), mPositionIconAvailable, mPositionIconOutput,
                layoutDirection);
        mIcon.setBounds(mPositionIconOutput);
    
public booleanprefersCondensedTitle()

        return true;
    
voidsetCaptionMode(boolean shortcut)

        /*
         * If there is no item model, don't do any of the below (for example,
         * the 'More' item doesn't have a model)
         */
        if (mItemData == null) {
            return;
        }
        
        mShortcutCaptionMode = shortcut && (mItemData.shouldShowShortcut());
        
        CharSequence text = mItemData.getTitleForItemView(this);
        
        if (mShortcutCaptionMode) {
            
            if (mShortcutCaption == null) {
                mShortcutCaption = mItemData.getShortcutLabel();
            }

            text = mShortcutCaption;
        }
        
        setText(text);
    
public voidsetCheckable(boolean checkable)

    
public voidsetChecked(boolean checked)

    
public voidsetIcon(android.graphics.drawable.Drawable icon)

        mIcon = icon;
        
        if (icon != null) {
            
            /* Set the bounds of the icon since setCompoundDrawables needs it. */
            icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
            
            // Set the compound drawables
            setCompoundDrawables(null, icon, null, null);
            
            // When there is an icon, make sure the text is at the bottom
            setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);

            /*
             * Request a layout to reposition the icon. The positioning of icon
             * depends on this TextView's line bounds, which is only available
             * after a layout.
             */  
            requestLayout();
        } else {
            setCompoundDrawables(null, null, null, null);
            
            // When there is no icon, make sure the text is centered vertically
            setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
        }
    
voidsetIconMenuView(IconMenuView iconMenuView)

        mIconMenuView = iconMenuView;
    
public voidsetItemData(MenuItemImpl data)

        mItemData = data;
    
public voidsetItemInvoker(com.android.internal.view.menu.MenuBuilder.ItemInvoker itemInvoker)

        mItemInvoker = itemInvoker;
    
public voidsetShortcut(boolean showShortcut, char shortcutKey)

        
        if (mShortcutCaptionMode) {
            /*
             * Shortcut has changed and we're showing it right now, need to
             * update (clear the old one first).
             */
            mShortcutCaption = null;
            setCaptionMode(true);
        }
    
public voidsetTitle(java.lang.CharSequence title)

        
        if (mShortcutCaptionMode) {
            /*
             * Don't set the title directly since it will replace the
             * shortcut+title being shown. Instead, re-set the shortcut caption
             * mode so the new title is shown.
             */
            setCaptionMode(true);
            
        } else if (title != null) {
            setText(title);
        }
    
public voidsetVisibility(int v)

        super.setVisibility(v);
        
        if (mIconMenuView != null) {
            // On visibility change, mark the IconMenuView to refresh itself eventually
            mIconMenuView.markStaleChildren();
        }
    
public booleanshowsIcon()

        return true;