2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.actionbarsherlock.internal.view.menu;
19 import com.actionbarsherlock.R;
21 import android.content.Context;
22 import android.content.res.TypedArray;
23 import android.graphics.drawable.Drawable;
24 import android.util.AttributeSet;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.CheckBox;
29 import android.widget.CompoundButton;
30 import android.widget.ImageView;
31 import android.widget.LinearLayout;
32 import android.widget.RadioButton;
33 import android.widget.TextView;
36 * The item view for each item in the ListView-based MenuViews.
38 public class ListMenuItemView extends LinearLayout implements MenuView.ItemView {
39 private MenuItemImpl mItemData;
41 private ImageView mIconView;
42 private RadioButton mRadioButton;
43 private TextView mTitleView;
44 private CheckBox mCheckBox;
45 private TextView mShortcutView;
47 private Drawable mBackground;
48 private int mTextAppearance;
49 private Context mTextAppearanceContext;
50 private boolean mPreserveIconSpacing;
52 //UNUSED private int mMenuType;
54 private LayoutInflater mInflater;
56 private boolean mForceShowIcon;
58 final Context mContext;
60 public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) {
61 super(context, attrs);
65 context.obtainStyledAttributes(
66 attrs, R.styleable.SherlockMenuView, defStyle, 0);
68 mBackground = a.getDrawable(R.styleable.SherlockMenuView_itemBackground);
69 mTextAppearance = a.getResourceId(R.styleable.
70 SherlockMenuView_itemTextAppearance, -1);
71 mPreserveIconSpacing = a.getBoolean(
72 R.styleable.SherlockMenuView_preserveIconSpacing, false);
73 mTextAppearanceContext = context;
78 public ListMenuItemView(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
83 protected void onFinishInflate() {
84 super.onFinishInflate();
86 setBackgroundDrawable(mBackground);
88 mTitleView = (TextView) findViewById(R.id.abs__title);
89 if (mTextAppearance != -1) {
90 mTitleView.setTextAppearance(mTextAppearanceContext,
94 mShortcutView = (TextView) findViewById(R.id.abs__shortcut);
97 public void initialize(MenuItemImpl itemData, int menuType) {
99 //UNUSED mMenuType = menuType;
101 setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
103 setTitle(itemData.getTitleForItemView(this));
104 setCheckable(itemData.isCheckable());
105 setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
106 setIcon(itemData.getIcon());
107 setEnabled(itemData.isEnabled());
110 public void setForceShowIcon(boolean forceShow) {
111 mPreserveIconSpacing = mForceShowIcon = forceShow;
114 public void setTitle(CharSequence title) {
116 mTitleView.setText(title);
118 if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE);
120 if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE);
124 public MenuItemImpl getItemData() {
128 public void setCheckable(boolean checkable) {
130 if (!checkable && mRadioButton == null && mCheckBox == null) {
134 if (mRadioButton == null) {
137 if (mCheckBox == null) {
141 // Depending on whether its exclusive check or not, the checkbox or
142 // radio button will be the one in use (and the other will be otherCompoundButton)
143 final CompoundButton compoundButton;
144 final CompoundButton otherCompoundButton;
146 if (mItemData.isExclusiveCheckable()) {
147 compoundButton = mRadioButton;
148 otherCompoundButton = mCheckBox;
150 compoundButton = mCheckBox;
151 otherCompoundButton = mRadioButton;
155 compoundButton.setChecked(mItemData.isChecked());
157 final int newVisibility = checkable ? VISIBLE : GONE;
158 if (compoundButton.getVisibility() != newVisibility) {
159 compoundButton.setVisibility(newVisibility);
162 // Make sure the other compound button isn't visible
163 if (otherCompoundButton.getVisibility() != GONE) {
164 otherCompoundButton.setVisibility(GONE);
167 mCheckBox.setVisibility(GONE);
168 mRadioButton.setVisibility(GONE);
172 public void setChecked(boolean checked) {
173 CompoundButton compoundButton;
175 if (mItemData.isExclusiveCheckable()) {
176 if (mRadioButton == null) {
179 compoundButton = mRadioButton;
181 if (mCheckBox == null) {
184 compoundButton = mCheckBox;
187 compoundButton.setChecked(checked);
190 public void setShortcut(boolean showShortcut, char shortcutKey) {
191 final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
194 if (newVisibility == VISIBLE) {
195 mShortcutView.setText(mItemData.getShortcutLabel());
198 if (mShortcutView.getVisibility() != newVisibility) {
199 mShortcutView.setVisibility(newVisibility);
203 public void setIcon(Drawable icon) {
204 final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
205 if (!showIcon && !mPreserveIconSpacing) {
209 if (mIconView == null && icon == null && !mPreserveIconSpacing) {
213 if (mIconView == null) {
217 if (icon != null || mPreserveIconSpacing) {
218 mIconView.setImageDrawable(showIcon ? icon : null);
220 if (mIconView.getVisibility() != VISIBLE) {
221 mIconView.setVisibility(VISIBLE);
224 mIconView.setVisibility(GONE);
229 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
230 if (mIconView != null && mPreserveIconSpacing) {
231 // Enforce minimum icon spacing
232 ViewGroup.LayoutParams lp = getLayoutParams();
233 LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
234 if (lp.height > 0 && iconLp.width <= 0) {
235 iconLp.width = lp.height;
238 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
241 private void insertIconView() {
242 LayoutInflater inflater = getInflater();
243 mIconView = (ImageView) inflater.inflate(R.layout.abs__list_menu_item_icon,
245 addView(mIconView, 0);
248 private void insertRadioButton() {
249 LayoutInflater inflater = getInflater();
251 (RadioButton) inflater.inflate(R.layout.abs__list_menu_item_radio,
253 addView(mRadioButton);
256 private void insertCheckBox() {
257 LayoutInflater inflater = getInflater();
259 (CheckBox) inflater.inflate(R.layout.abs__list_menu_item_checkbox,
264 public boolean prefersCondensedTitle() {
268 public boolean showsIcon() {
269 return mForceShowIcon;
272 private LayoutInflater getInflater() {
273 if (mInflater == null) {
274 mInflater = LayoutInflater.from(mContext);