NavigationMenuItemView.java revision a63940ca14cd3ad9620e94f709930bb968525c57
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.design.internal;
18
19import android.content.Context;
20import android.content.res.ColorStateList;
21import android.graphics.Color;
22import android.graphics.drawable.ColorDrawable;
23import android.graphics.drawable.Drawable;
24import android.graphics.drawable.StateListDrawable;
25import android.support.design.R;
26import android.support.v4.graphics.drawable.DrawableCompat;
27import android.support.v4.widget.TextViewCompat;
28import android.support.v7.internal.view.menu.MenuItemImpl;
29import android.support.v7.internal.view.menu.MenuView;
30import android.util.AttributeSet;
31import android.util.TypedValue;
32import android.widget.TextView;
33
34/**
35 * @hide
36 */
37public class NavigationMenuItemView extends TextView implements MenuView.ItemView {
38
39    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
40
41    private int mIconSize;
42    private MenuItemImpl mItemData;
43    private ColorStateList mIconTintList;
44
45    public NavigationMenuItemView(Context context) {
46        this(context, null);
47    }
48
49    public NavigationMenuItemView(Context context, AttributeSet attrs) {
50        this(context, attrs, 0);
51    }
52
53    public NavigationMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
54        super(context, attrs, defStyleAttr);
55        mIconSize = context.getResources().getDimensionPixelSize(R.dimen.navigation_icon_size);
56    }
57
58    @Override
59    public void initialize(MenuItemImpl itemData, int menuType) {
60        mItemData = itemData;
61
62        setVisibility(itemData.isVisible() ? VISIBLE : GONE);
63
64        if (getBackground() == null) {
65            setBackgroundDrawable(createDefaultBackground());
66        }
67
68        setCheckable(itemData.isCheckable());
69        setChecked(itemData.isChecked());
70        setEnabled(itemData.isEnabled());
71        setTitle(itemData.getTitle());
72        setIcon(itemData.getIcon());
73    }
74
75    private StateListDrawable createDefaultBackground() {
76        TypedValue value = new TypedValue();
77        if (getContext().getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true)) {
78            StateListDrawable drawable = new StateListDrawable();
79            drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data));
80            drawable.addState(EMPTY_STATE_SET, new ColorDrawable(Color.TRANSPARENT));
81            return drawable;
82        }
83        return null;
84    }
85
86    @Override
87    public MenuItemImpl getItemData() {
88        return mItemData;
89    }
90
91    @Override
92    public void setTitle(CharSequence title) {
93        setText(title);
94    }
95
96    @Override
97    public void setCheckable(boolean checkable) {
98        refreshDrawableState();
99    }
100
101    @Override
102    public void setChecked(boolean checked) {
103        refreshDrawableState();
104    }
105
106    @Override
107    public void setShortcut(boolean showShortcut, char shortcutKey) {
108    }
109
110    @Override
111    public void setIcon(Drawable icon) {
112        if (icon != null) {
113            icon = DrawableCompat.wrap(icon);
114            icon = icon.mutate();
115            icon.setBounds(0, 0, mIconSize, mIconSize);
116            DrawableCompat.setTintList(icon, mIconTintList);
117        }
118        TextViewCompat.setCompoundDrawablesRelative(this, icon, null, null, null);
119    }
120
121    @Override
122    public boolean prefersCondensedTitle() {
123        return false;
124    }
125
126    @Override
127    public boolean showsIcon() {
128        return true;
129    }
130
131    @Override
132    protected int[] onCreateDrawableState(int extraSpace) {
133        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
134        if (mItemData != null && mItemData.isCheckable() && mItemData.isChecked()) {
135            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
136        }
137        return drawableState;
138    }
139
140    void setIconTintList(ColorStateList tintList) {
141        mIconTintList = tintList;
142        if (mItemData != null) {
143            // Update the icon so that the tint takes effect
144            setIcon(mItemData.getIcon());
145        }
146    }
147}
148