ListMenuItemView.java revision 6142a54baae3289f734947c6b5375b12eb0fb722
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 1766698bb15ba0f873aa1c2290cc50d6bb839a474aChris Banespackage android.support.v7.view.menu; 18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 19bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Context; 20bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.graphics.drawable.Drawable; 21da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownimport android.support.v7.appcompat.R; 226142a54baae3289f734947c6b5375b12eb0fb722Chris Banesimport android.support.v7.widget.TintTypedArray; 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. 3689208232f3b5d1451408d787872504a190bc7ee0Chris Banes * 3789208232f3b5d1451408d787872504a190bc7ee0Chris Banes * @hide 38bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */ 39bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellpublic class ListMenuItemView extends LinearLayout implements MenuView.ItemView { 40ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private static final String TAG = "ListMenuItemView"; 41ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private MenuItemImpl mItemData; 42bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 43ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private ImageView mIconView; 44ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private RadioButton mRadioButton; 45ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private TextView mTitleView; 46ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private CheckBox mCheckBox; 47ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private TextView mShortcutView; 486142a54baae3289f734947c6b5375b12eb0fb722Chris Banes private ImageView mSubMenuArrowView; 49bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 50ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private Drawable mBackground; 51ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private int mTextAppearance; 52ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private Context mTextAppearanceContext; 53ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private boolean mPreserveIconSpacing; 546142a54baae3289f734947c6b5375b12eb0fb722Chris Banes private Drawable mSubMenuArrow; 55bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 56ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private int mMenuType; 57bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 58ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private LayoutInflater mInflater; 59bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 60ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private boolean mForceShowIcon; 61bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 626142a54baae3289f734947c6b5375b12eb0fb722Chris Banes public ListMenuItemView(Context context, AttributeSet attrs) { 636142a54baae3289f734947c6b5375b12eb0fb722Chris Banes this(context, attrs, R.attr.listMenuViewStyle); 646142a54baae3289f734947c6b5375b12eb0fb722Chris Banes } 656142a54baae3289f734947c6b5375b12eb0fb722Chris Banes 666142a54baae3289f734947c6b5375b12eb0fb722Chris Banes public ListMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) { 67ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani super(context, attrs); 68bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 696142a54baae3289f734947c6b5375b12eb0fb722Chris Banes final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), 706142a54baae3289f734947c6b5375b12eb0fb722Chris Banes attrs, R.styleable.MenuView, defStyleAttr, 0); 71bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns mBackground = a.getDrawable(R.styleable.MenuView_android_itemBackground); 73ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mTextAppearance = a.getResourceId(R.styleable. 748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns MenuView_android_itemTextAppearance, -1); 75ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mPreserveIconSpacing = a.getBoolean( 76692b70462703c0c0c9e6c5dec315a9aa783b5f55Chris Banes R.styleable.MenuView_preserveIconSpacing, false); 77ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mTextAppearanceContext = context; 786142a54baae3289f734947c6b5375b12eb0fb722Chris Banes mSubMenuArrow = a.getDrawable(R.styleable.MenuView_subMenuArrow); 79bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 80ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani a.recycle(); 81ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 82bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 83ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani @Override 84ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani protected void onFinishInflate() { 85ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani super.onFinishInflate(); 86bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 87ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani setBackgroundDrawable(mBackground); 88bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 89ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mTitleView = (TextView) findViewById(R.id.title); 90ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mTextAppearance != -1) { 91ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mTitleView.setTextAppearance(mTextAppearanceContext, 9220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns mTextAppearance); 93ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 94bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 95ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mShortcutView = (TextView) findViewById(R.id.shortcut); 966142a54baae3289f734947c6b5375b12eb0fb722Chris Banes mSubMenuArrowView = (ImageView) findViewById(R.id.submenuarrow); 976142a54baae3289f734947c6b5375b12eb0fb722Chris Banes if (mSubMenuArrowView != null) { 986142a54baae3289f734947c6b5375b12eb0fb722Chris Banes mSubMenuArrowView.setImageDrawable(mSubMenuArrow); 996142a54baae3289f734947c6b5375b12eb0fb722Chris Banes } 100ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 101bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 102ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public void initialize(MenuItemImpl itemData, int menuType) { 103ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mItemData = itemData; 104ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mMenuType = menuType; 105bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 106ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); 107bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 108ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani setTitle(itemData.getTitleForItemView(this)); 109ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani setCheckable(itemData.isCheckable()); 110ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut()); 111ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani setIcon(itemData.getIcon()); 112ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani setEnabled(itemData.isEnabled()); 1136142a54baae3289f734947c6b5375b12eb0fb722Chris Banes setSubMenuArrowVisible(itemData.hasSubMenu()); 114bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 115bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 116ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public void setForceShowIcon(boolean forceShow) { 117ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mPreserveIconSpacing = mForceShowIcon = forceShow; 118ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 119ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 120ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public void setTitle(CharSequence title) { 121ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (title != null) { 122ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mTitleView.setText(title); 123bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 12449c78900da0d43140fb602431fb93212bd7f6c70Chris Banes if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE); 125ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } else { 12649c78900da0d43140fb602431fb93212bd7f6c70Chris Banes if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE); 127ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 128bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 129bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 130ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public MenuItemImpl getItemData() { 131ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani return mItemData; 132bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 133bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 134ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public void setCheckable(boolean checkable) { 135ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (!checkable && mRadioButton == null && mCheckBox == null) { 136ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani return; 137ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 138ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 139ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani // Depending on whether its exclusive check or not, the checkbox or 140ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani // radio button will be the one in use (and the other will be otherCompoundButton) 141ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani final CompoundButton compoundButton; 142ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani final CompoundButton otherCompoundButton; 143ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 144ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mItemData.isExclusiveCheckable()) { 145ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mRadioButton == null) { 146ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani insertRadioButton(); 147ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 148ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani compoundButton = mRadioButton; 149ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani otherCompoundButton = mCheckBox; 150ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } else { 151ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mCheckBox == null) { 152ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani insertCheckBox(); 153ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 154ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani compoundButton = mCheckBox; 155ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani otherCompoundButton = mRadioButton; 156ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 157ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 158ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (checkable) { 159ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani compoundButton.setChecked(mItemData.isChecked()); 160ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 161ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani final int newVisibility = checkable ? VISIBLE : GONE; 162ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (compoundButton.getVisibility() != newVisibility) { 163ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani compoundButton.setVisibility(newVisibility); 164ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 165ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 166ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani // Make sure the other compound button isn't visible 167ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (otherCompoundButton != null && otherCompoundButton.getVisibility() != GONE) { 168ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani otherCompoundButton.setVisibility(GONE); 169ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 170ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } else { 17120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns if (mCheckBox != null) { 17220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns mCheckBox.setVisibility(GONE); 17320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns } 17420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns if (mRadioButton != null) { 17520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns mRadioButton.setVisibility(GONE); 17620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns } 177ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 178bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 179ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 180ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public void setChecked(boolean checked) { 181ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani CompoundButton compoundButton; 182ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 183ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mItemData.isExclusiveCheckable()) { 184ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mRadioButton == null) { 185ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani insertRadioButton(); 186ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 187ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani compoundButton = mRadioButton; 188ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } else { 189ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mCheckBox == null) { 190ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani insertCheckBox(); 191ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 192ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani compoundButton = mCheckBox; 193ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 194ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 195ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani compoundButton.setChecked(checked); 196bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 197bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 1986142a54baae3289f734947c6b5375b12eb0fb722Chris Banes private void setSubMenuArrowVisible(boolean hasSubmenu) { 1996142a54baae3289f734947c6b5375b12eb0fb722Chris Banes if (mSubMenuArrowView != null) { 2006142a54baae3289f734947c6b5375b12eb0fb722Chris Banes mSubMenuArrowView.setVisibility(hasSubmenu ? View.VISIBLE : View.GONE); 2016142a54baae3289f734947c6b5375b12eb0fb722Chris Banes } 2026142a54baae3289f734947c6b5375b12eb0fb722Chris Banes } 2036142a54baae3289f734947c6b5375b12eb0fb722Chris Banes 204ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public void setShortcut(boolean showShortcut, char shortcutKey) { 205ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani final int newVisibility = (showShortcut && mItemData.shouldShowShortcut()) 20620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns ? VISIBLE : GONE; 207bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 208ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (newVisibility == VISIBLE) { 209ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mShortcutView.setText(mItemData.getShortcutLabel()); 210ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 211bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 212ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mShortcutView.getVisibility() != newVisibility) { 213ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mShortcutView.setVisibility(newVisibility); 214ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 215bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 216bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 217ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public void setIcon(Drawable icon) { 218ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon; 219ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (!showIcon && !mPreserveIconSpacing) { 220ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani return; 221ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 222ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 223ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mIconView == null && icon == null && !mPreserveIconSpacing) { 224ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani return; 225ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 226ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 227ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mIconView == null) { 228ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani insertIconView(); 229ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 230ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 231ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (icon != null || mPreserveIconSpacing) { 232ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mIconView.setImageDrawable(showIcon ? icon : null); 233ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 234ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mIconView.getVisibility() != VISIBLE) { 235ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mIconView.setVisibility(VISIBLE); 236ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 237ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } else { 238ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mIconView.setVisibility(GONE); 239ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 240bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 241bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 242ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani @Override 243ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 244ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mIconView != null && mPreserveIconSpacing) { 245ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani // Enforce minimum icon spacing 246ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani ViewGroup.LayoutParams lp = getLayoutParams(); 247ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams(); 248ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (lp.height > 0 && iconLp.width <= 0) { 249ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani iconLp.width = lp.height; 250ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 251ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 252ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani super.onMeasure(widthMeasureSpec, heightMeasureSpec); 253bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 254bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 255ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private void insertIconView() { 256ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani LayoutInflater inflater = getInflater(); 257ee7c9fb199e9b9af8d40a1f9e27d85465acf8301Chris Banes mIconView = (ImageView) inflater.inflate(R.layout.abc_list_menu_item_icon, 25820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns this, false); 259ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani addView(mIconView, 0); 260bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 261bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 262ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private void insertRadioButton() { 263ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani LayoutInflater inflater = getInflater(); 264ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mRadioButton = 265ee7c9fb199e9b9af8d40a1f9e27d85465acf8301Chris Banes (RadioButton) inflater.inflate(R.layout.abc_list_menu_item_radio, 26620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns this, false); 267ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani addView(mRadioButton); 268bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 269bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 270ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private void insertCheckBox() { 271ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani LayoutInflater inflater = getInflater(); 272ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani mCheckBox = 273ee7c9fb199e9b9af8d40a1f9e27d85465acf8301Chris Banes (CheckBox) inflater.inflate(R.layout.abc_list_menu_item_checkbox, 27420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns this, false); 275ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani addView(mCheckBox); 276bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 277bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 278ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public boolean prefersCondensedTitle() { 279ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani return false; 280ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 281bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 282ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani public boolean showsIcon() { 283ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani return mForceShowIcon; 284ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 285bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell 286ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani private LayoutInflater getInflater() { 287ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani if (mInflater == null) { 2886142a54baae3289f734947c6b5375b12eb0fb722Chris Banes mInflater = LayoutInflater.from(getContext()); 289ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani } 290ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani return mInflater; 291bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell } 292bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell} 293ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani 294