ActionMenuItemView.java revision 6bddd8771d05889024778caa78fb1eaae68a0802
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.ImageButton; 25import android.widget.LinearLayout; 26 27/** 28 * @hide 29 */ 30public class ActionMenuItemView extends LinearLayout 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 super(context, attrs); 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 setOnClickListener(this); 60 } 61 62 public MenuItemImpl getItemData() { 63 return mItemData; 64 } 65 66 public void initialize(MenuItemImpl itemData, int menuType) { 67 mItemData = itemData; 68 69 setIcon(itemData.getIcon()); 70 setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon 71 setId(itemData.getItemId()); 72 73 setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); 74 setEnabled(itemData.isEnabled()); 75 } 76 77 @Override 78 public void setEnabled(boolean enabled) { 79 super.setEnabled(enabled); 80 mImageButton.setEnabled(enabled); 81 mTextButton.setEnabled(enabled); 82 } 83 84 public void onClick(View v) { 85 if (mItemInvoker != null) { 86 mItemInvoker.invokeItem(mItemData); 87 } 88 } 89 90 public void setItemInvoker(MenuBuilder.ItemInvoker invoker) { 91 mItemInvoker = invoker; 92 } 93 94 public boolean prefersCondensedTitle() { 95 return true; 96 } 97 98 public void setCheckable(boolean checkable) { 99 // TODO Support checkable action items 100 } 101 102 public void setChecked(boolean checked) { 103 // TODO Support checkable action items 104 } 105 106 public void setIcon(Drawable icon) { 107 mImageButton.setImageDrawable(icon); 108 if (icon != null) { 109 mImageButton.setVisibility(VISIBLE); 110 } else { 111 mImageButton.setVisibility(GONE); 112 } 113 114 mTextButton.setVisibility(icon == null || mItemData.showsTextAsAction() ? VISIBLE : GONE); 115 } 116 117 public boolean hasText() { 118 return mTextButton.getVisibility() != GONE; 119 } 120 121 public void setShortcut(boolean showShortcut, char shortcutKey) { 122 // Action buttons don't show text for shortcut keys. 123 } 124 125 public void setTitle(CharSequence title) { 126 mTitle = title; 127 128 // populate accessibility description with title 129 setContentDescription(title); 130 131 if (mImageButton.getDrawable() == null || mItemData.showsTextAsAction()) { 132 mTextButton.setText(mTitle); 133 mTextButton.setVisibility(VISIBLE); 134 } 135 } 136 137 public boolean showsIcon() { 138 return true; 139 } 140} 141