ListMenuItemView.java revision ced50ab7536cd3d3573f03310fc899f10c414d37
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
17bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellpackage android.support.appcompat.view.menu;
18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
19bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Context;
20bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.res.TypedArray;
21bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.graphics.drawable.Drawable;
22bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.support.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 {
38ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final String TAG = "ListMenuItemView";
39ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private MenuItemImpl mItemData;
40bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
41ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private ImageView mIconView;
42ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private RadioButton mRadioButton;
43ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private TextView mTitleView;
44ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private CheckBox mCheckBox;
45ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private TextView mShortcutView;
46bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
47ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Drawable mBackground;
48ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mTextAppearance;
49ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Context mTextAppearanceContext;
50ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mPreserveIconSpacing;
51bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
52ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mMenuType;
53bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
54ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Context mContext;
55ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private LayoutInflater mInflater;
56bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
57ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mForceShowIcon;
58bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
59ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) {
60ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super(context, attrs);
61ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mContext = context;
62bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
63ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        TypedArray a =
64ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            context.obtainStyledAttributes(
65ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                attrs, R.styleable.MenuView, defStyle, 0);
66bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
67ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mBackground = a.getDrawable(R.styleable.MenuView_itemBackground);
68ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTextAppearance = a.getResourceId(R.styleable.
69ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            MenuView_itemTextAppearance, -1);
70ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mPreserveIconSpacing = a.getBoolean(
71ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            R.styleable.MenuView_preserveIconSpacing, false);
72ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTextAppearanceContext = context;
73bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
74ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        a.recycle();
75ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
76bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
77ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ListMenuItemView(Context context, AttributeSet attrs) {
78ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        this(context, attrs, 0);
79bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
80bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
81ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
82ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    protected void onFinishInflate() {
83ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.onFinishInflate();
84bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
85ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setBackgroundDrawable(mBackground);
86bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
87ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTitleView = (TextView) findViewById(R.id.title);
88ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mTextAppearance != -1) {
89ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mTitleView.setTextAppearance(mTextAppearanceContext,
90ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                mTextAppearance);
91ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
92bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
93ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShortcutView = (TextView) findViewById(R.id.shortcut);
94ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
95bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
96ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void initialize(MenuItemImpl itemData, int menuType) {
97ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mItemData = itemData;
98ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mMenuType = menuType;
99bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
100ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
101bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
102ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setTitle(itemData.getTitleForItemView(this));
103ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setCheckable(itemData.isCheckable());
104ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
105ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setIcon(itemData.getIcon());
106ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setEnabled(itemData.isEnabled());
107bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
108bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
109ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setForceShowIcon(boolean forceShow) {
110ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mPreserveIconSpacing = mForceShowIcon = forceShow;
111ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
112ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
113ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setTitle(CharSequence title) {
114ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (title != null) {
115ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mTitleView.setText(title);
116bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
117ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE);
118ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
119ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE);
120ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
121bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
122bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
123ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItemImpl getItemData() {
124ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mItemData;
125bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
126bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
127ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setCheckable(boolean checkable) {
128ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (!checkable && mRadioButton == null && mCheckBox == null) {
129ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
130ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
131ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
132ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // Depending on whether its exclusive check or not, the checkbox or
133ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // radio button will be the one in use (and the other will be otherCompoundButton)
134ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final CompoundButton compoundButton;
135ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final CompoundButton otherCompoundButton;
136ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
137ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemData.isExclusiveCheckable()) {
138ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mRadioButton == null) {
139ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertRadioButton();
140ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
141ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mRadioButton;
142ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            otherCompoundButton = mCheckBox;
143ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
144ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mCheckBox == null) {
145ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertCheckBox();
146ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
147ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mCheckBox;
148ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            otherCompoundButton = mRadioButton;
149ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
150ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
151ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (checkable) {
152ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton.setChecked(mItemData.isChecked());
153ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
154ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            final int newVisibility = checkable ? VISIBLE : GONE;
155ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (compoundButton.getVisibility() != newVisibility) {
156ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                compoundButton.setVisibility(newVisibility);
157ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
158ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
159ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Make sure the other compound button isn't visible
160ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (otherCompoundButton != null && otherCompoundButton.getVisibility() != GONE) {
161ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                otherCompoundButton.setVisibility(GONE);
162ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
163ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
164ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mCheckBox != null) mCheckBox.setVisibility(GONE);
165ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mRadioButton != null) mRadioButton.setVisibility(GONE);
166ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
167bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
168ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
169ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setChecked(boolean checked) {
170ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        CompoundButton compoundButton;
171ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
172ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemData.isExclusiveCheckable()) {
173ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mRadioButton == null) {
174ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertRadioButton();
175ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
176ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mRadioButton;
177ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
178ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mCheckBox == null) {
179ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertCheckBox();
180ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
181ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mCheckBox;
182ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
183ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
184ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        compoundButton.setChecked(checked);
185bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
186bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
187ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setShortcut(boolean showShortcut, char shortcutKey) {
188ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
189ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            ? VISIBLE : GONE;
190bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
191ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (newVisibility == VISIBLE) {
192ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mShortcutView.setText(mItemData.getShortcutLabel());
193ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
194bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
195ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mShortcutView.getVisibility() != newVisibility) {
196ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mShortcutView.setVisibility(newVisibility);
197ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
198bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
199bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
200ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setIcon(Drawable icon) {
201ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
202ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (!showIcon && !mPreserveIconSpacing) {
203ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
204ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
205ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
206ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView == null && icon == null && !mPreserveIconSpacing) {
207ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
208ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
209ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
210ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView == null) {
211ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            insertIconView();
212ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
213ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
214ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (icon != null || mPreserveIconSpacing) {
215ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mIconView.setImageDrawable(showIcon ? icon : null);
216ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
217ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mIconView.getVisibility() != VISIBLE) {
218ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                mIconView.setVisibility(VISIBLE);
219ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
220ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
221ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mIconView.setVisibility(GONE);
222ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
223bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
224bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
225ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
226ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
227ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView != null && mPreserveIconSpacing) {
228ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Enforce minimum icon spacing
229ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            ViewGroup.LayoutParams lp = getLayoutParams();
230ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
231ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (lp.height > 0 && iconLp.width <= 0) {
232ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                iconLp.width = lp.height;
233ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
234ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
235ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
236bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
237bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
238ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertIconView() {
239ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
240ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIconView = (ImageView) inflater.inflate(R.layout.list_menu_item_icon,
241ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            this, false);
242ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mIconView, 0);
243bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
244bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
245ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertRadioButton() {
246ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
247ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mRadioButton =
248ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            (RadioButton) inflater.inflate(R.layout.list_menu_item_radio,
249ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                this, false);
250ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mRadioButton);
251bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
252bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
253ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertCheckBox() {
254ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
255ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mCheckBox =
256ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            (CheckBox) inflater.inflate(R.layout.list_menu_item_checkbox,
257ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                this, false);
258ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mCheckBox);
259bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
260bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
261ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean prefersCondensedTitle() {
262ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return false;
263ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
264bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
265ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean showsIcon() {
266ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mForceShowIcon;
267ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
268bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
269ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private LayoutInflater getInflater() {
270ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mInflater == null) {
271ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mInflater = LayoutInflater.from(mContext);
272ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
273ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mInflater;
274bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
275bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell}
276ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
277