ActionMenuItemView.java revision 00df32d4396a65752b7b9a2f8ceb6292ed2384cf
1/* 2 * Copyright (C) 2010 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 com.android.internal.view.menu; 18 19import android.content.Context; 20import android.graphics.drawable.Drawable; 21import android.util.AttributeSet; 22import android.view.View; 23import android.widget.Button; 24import android.widget.FrameLayout; 25import android.widget.ImageButton; 26 27/** 28 * @hide 29 */ 30public class ActionMenuItemView extends FrameLayout 31 implements MenuView.ItemView, View.OnClickListener { 32 private static final String TAG = "ActionMenuItemView"; 33 34 private MenuItemImpl mItemData; 35 private CharSequence mTitle; 36 private MenuBuilder.ItemInvoker mItemInvoker; 37 38 private ImageButton mImageButton; 39 private Button mTextButton; 40 41 public ActionMenuItemView(Context context) { 42 this(context, null); 43 } 44 45 public ActionMenuItemView(Context context, AttributeSet attrs) { 46 this(context, attrs, com.android.internal.R.attr.actionButtonStyle); 47 } 48 49 public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 } 52 53 @Override 54 public void onFinishInflate() { 55 mImageButton = (ImageButton) findViewById(com.android.internal.R.id.imageButton); 56 mTextButton = (Button) findViewById(com.android.internal.R.id.textButton); 57 mImageButton.setOnClickListener(this); 58 mTextButton.setOnClickListener(this); 59 } 60 61 public MenuItemImpl getItemData() { 62 return mItemData; 63 } 64 65 public void initialize(MenuItemImpl itemData, int menuType) { 66 mItemData = itemData; 67 68 setClickable(true); 69 setFocusable(true); 70 setIcon(itemData.getIcon()); 71 setTitle(itemData.getTitle()); // Title only takes effect if there is no icon 72 setId(itemData.getItemId()); 73 74 setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); 75 setEnabled(itemData.isEnabled()); 76 } 77 78 @Override 79 public void setEnabled(boolean enabled) { 80 super.setEnabled(enabled); 81 mImageButton.setEnabled(enabled); 82 mTextButton.setEnabled(enabled); 83 } 84 85 public void onClick(View v) { 86 if (mItemInvoker != null) { 87 mItemInvoker.invokeItem(mItemData); 88 } 89 } 90 91 public void setItemInvoker(MenuBuilder.ItemInvoker invoker) { 92 mItemInvoker = invoker; 93 } 94 95 public boolean prefersCondensedTitle() { 96 return false; 97 } 98 99 public void setCheckable(boolean checkable) { 100 // TODO Support checkable action items 101 } 102 103 public void setChecked(boolean checked) { 104 // TODO Support checkable action items 105 } 106 107 public void setIcon(Drawable icon) { 108 mImageButton.setImageDrawable(icon); 109 if (icon != null) { 110 mImageButton.setVisibility(VISIBLE); 111 mTextButton.setVisibility(GONE); 112 } else { 113 mImageButton.setVisibility(GONE); 114 } 115 } 116 117 public void setShortcut(boolean showShortcut, char shortcutKey) { 118 // Action buttons don't show text for shortcut keys. 119 } 120 121 public void setTitle(CharSequence title) { 122 mTitle = title; 123 124 // populate accessibility description with title 125 setContentDescription(title); 126 127 if (mImageButton.getDrawable() == null) { 128 mTextButton.setText(mTitle); 129 mTextButton.setVisibility(VISIBLE); 130 } 131 } 132 133 public boolean showsIcon() { 134 return true; 135 } 136} 137