ListMenuItemView.java revision da10fdd1400ecfd8d7f2e55651dd528d0614dfc5
1bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/*
2bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Copyright (C) 2006 The Android Open Source Project
3bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
4bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * you may not use this file except in compliance with the License.
6bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * You may obtain a copy of the License at
7bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
8bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
10bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Unless required by applicable law or agreed to in writing, software
11bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * See the License for the specific language governing permissions and
14bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * limitations under the License.
15bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
16bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
17da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownpackage android.support.v7.internal.view.menu;
18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
19bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Context;
20bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.res.TypedArray;
21bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.graphics.drawable.Drawable;
22da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownimport android.support.v7.appcompat.R;
23bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.util.AttributeSet;
24bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.LayoutInflater;
25bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.View;
26bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.ViewGroup;
27bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.CheckBox;
28bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.CompoundButton;
29bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.ImageView;
30bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.LinearLayout;
31bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.RadioButton;
32bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.TextView;
33bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
34bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/**
35bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * The item view for each item in the ListView-based MenuViews.
36bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
37bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellpublic class ListMenuItemView extends LinearLayout implements MenuView.ItemView {
3820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns
39ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final String TAG = "ListMenuItemView";
40ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private MenuItemImpl mItemData;
41bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
42ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private ImageView mIconView;
43ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private RadioButton mRadioButton;
44ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private TextView mTitleView;
45ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private CheckBox mCheckBox;
46ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private TextView mShortcutView;
47bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
48ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Drawable mBackground;
49ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mTextAppearance;
50ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Context mTextAppearanceContext;
51ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mPreserveIconSpacing;
52bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
53ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mMenuType;
54bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
55ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Context mContext;
56ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private LayoutInflater mInflater;
57bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
58ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mForceShowIcon;
59bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
60ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) {
61ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super(context, attrs);
62ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mContext = context;
63bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
64ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        TypedArray a =
6520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                context.obtainStyledAttributes(
6620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                        attrs, R.styleable.MenuView, defStyle, 0);
67bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        mBackground = a.getDrawable(R.styleable.MenuView_android_itemBackground);
69ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTextAppearance = a.getResourceId(R.styleable.
708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                MenuView_android_itemTextAppearance, -1);
71ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mPreserveIconSpacing = a.getBoolean(
728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                R.styleable.MenuView_android_preserveIconSpacing, false);
73ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTextAppearanceContext = context;
74bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
75ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        a.recycle();
76ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
77bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
78ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ListMenuItemView(Context context, AttributeSet attrs) {
79ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        this(context, attrs, 0);
80bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
81bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
82ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
83ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    protected void onFinishInflate() {
84ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.onFinishInflate();
85bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
86ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setBackgroundDrawable(mBackground);
87bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
88ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTitleView = (TextView) findViewById(R.id.title);
89ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mTextAppearance != -1) {
90ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mTitleView.setTextAppearance(mTextAppearanceContext,
9120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                    mTextAppearance);
92ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
93bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
94ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShortcutView = (TextView) findViewById(R.id.shortcut);
95ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
96bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
97ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void initialize(MenuItemImpl itemData, int menuType) {
98ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mItemData = itemData;
99ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mMenuType = menuType;
100bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
101ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
102bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
103ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setTitle(itemData.getTitleForItemView(this));
104ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setCheckable(itemData.isCheckable());
105ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
106ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setIcon(itemData.getIcon());
107ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setEnabled(itemData.isEnabled());
108bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
109bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
110ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setForceShowIcon(boolean forceShow) {
111ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mPreserveIconSpacing = mForceShowIcon = forceShow;
112ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
113ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
114ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setTitle(CharSequence title) {
115ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (title != null) {
116ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mTitleView.setText(title);
117bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
11820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            if (mTitleView.getVisibility() != VISIBLE) {
11920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mTitleView.setVisibility(VISIBLE);
12020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            }
121ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
12220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            if (mTitleView.getVisibility() != GONE) {
12320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mTitleView.setVisibility(GONE);
12420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            }
125ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
126bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
127bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
128ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItemImpl getItemData() {
129ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mItemData;
130bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
131bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
132ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setCheckable(boolean checkable) {
133ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (!checkable && mRadioButton == null && mCheckBox == null) {
134ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
135ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
136ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
137ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // Depending on whether its exclusive check or not, the checkbox or
138ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // radio button will be the one in use (and the other will be otherCompoundButton)
139ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final CompoundButton compoundButton;
140ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final CompoundButton otherCompoundButton;
141ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
142ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemData.isExclusiveCheckable()) {
143ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mRadioButton == null) {
144ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertRadioButton();
145ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
146ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mRadioButton;
147ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            otherCompoundButton = mCheckBox;
148ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
149ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mCheckBox == null) {
150ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertCheckBox();
151ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
152ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mCheckBox;
153ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            otherCompoundButton = mRadioButton;
154ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
155ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
156ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (checkable) {
157ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton.setChecked(mItemData.isChecked());
158ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
159ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            final int newVisibility = checkable ? VISIBLE : GONE;
160ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (compoundButton.getVisibility() != newVisibility) {
161ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                compoundButton.setVisibility(newVisibility);
162ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
163ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
164ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Make sure the other compound button isn't visible
165ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (otherCompoundButton != null && otherCompoundButton.getVisibility() != GONE) {
166ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                otherCompoundButton.setVisibility(GONE);
167ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
168ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
16920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            if (mCheckBox != null) {
17020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mCheckBox.setVisibility(GONE);
17120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            }
17220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            if (mRadioButton != null) {
17320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mRadioButton.setVisibility(GONE);
17420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            }
175ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
176bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
177ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
178ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setChecked(boolean checked) {
179ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        CompoundButton compoundButton;
180ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
181ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemData.isExclusiveCheckable()) {
182ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mRadioButton == null) {
183ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertRadioButton();
184ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
185ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mRadioButton;
186ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
187ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mCheckBox == null) {
188ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertCheckBox();
189ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
190ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mCheckBox;
191ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
192ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
193ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        compoundButton.setChecked(checked);
194bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
195bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
196ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setShortcut(boolean showShortcut, char shortcutKey) {
197ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
19820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                ? VISIBLE : GONE;
199bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
200ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (newVisibility == VISIBLE) {
201ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mShortcutView.setText(mItemData.getShortcutLabel());
202ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
203bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
204ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mShortcutView.getVisibility() != newVisibility) {
205ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mShortcutView.setVisibility(newVisibility);
206ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
207bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
208bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
209ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setIcon(Drawable icon) {
210ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
211ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (!showIcon && !mPreserveIconSpacing) {
212ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
213ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
214ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
215ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView == null && icon == null && !mPreserveIconSpacing) {
216ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
217ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
218ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
219ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView == null) {
220ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            insertIconView();
221ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
222ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
223ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (icon != null || mPreserveIconSpacing) {
224ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mIconView.setImageDrawable(showIcon ? icon : null);
225ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
226ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mIconView.getVisibility() != VISIBLE) {
227ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                mIconView.setVisibility(VISIBLE);
228ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
229ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
230ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mIconView.setVisibility(GONE);
231ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
232bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
233bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
234ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
235ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
236ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView != null && mPreserveIconSpacing) {
237ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Enforce minimum icon spacing
238ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            ViewGroup.LayoutParams lp = getLayoutParams();
239ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
240ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (lp.height > 0 && iconLp.width <= 0) {
241ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                iconLp.width = lp.height;
242ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
243ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
244ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
245bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
246bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
247ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertIconView() {
248ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
249ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIconView = (ImageView) inflater.inflate(R.layout.list_menu_item_icon,
25020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                this, false);
251ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mIconView, 0);
252bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
253bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
254ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertRadioButton() {
255ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
256ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mRadioButton =
25720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                (RadioButton) inflater.inflate(R.layout.list_menu_item_radio,
25820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                        this, false);
259ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mRadioButton);
260bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
261bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
262ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertCheckBox() {
263ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
264ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mCheckBox =
26520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                (CheckBox) inflater.inflate(R.layout.list_menu_item_checkbox,
26620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                        this, false);
267ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mCheckBox);
268bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
269bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
270ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean prefersCondensedTitle() {
271ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return false;
272ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
273bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
274ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean showsIcon() {
275ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mForceShowIcon;
276ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
277bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
278ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private LayoutInflater getInflater() {
279ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mInflater == null) {
280ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mInflater = LayoutInflater.from(mContext);
281ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
282ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mInflater;
283bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
284bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell}
285ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
286