ActionMenuItemView.java revision 367ee326058bbee6fc179b8b1eb2174fe7ba8f45
196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/* 296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Copyright (C) 2010 The Android Open Source Project 396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * you may not use this file except in compliance with the License. 696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * You may obtain a copy of the License at 796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * http://www.apache.org/licenses/LICENSE-2.0 996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * 1096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * Unless required by applicable law or agreed to in writing, software 1196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * distributed under the License is distributed on an "AS IS" BASIS, 1296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * See the License for the specific language governing permissions and 1496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * limitations under the License. 1596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */ 1696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 1796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellpackage com.android.internal.view.menu; 1896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 1996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.content.Context; 2035aecd5884a5ccfe380903e39f30f468315e8f92Adam Powellimport android.content.res.Resources; 2124340afbab8ad4c191784fda8023407205bc0a75Adam Powellimport android.content.res.TypedArray; 227b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.graphics.Rect; 2396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.graphics.drawable.Drawable; 24c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunneimport android.text.TextUtils; 2596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.util.AttributeSet; 267b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.view.Gravity; 277bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powellimport android.view.MotionEvent; 2896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powellimport android.view.View; 297bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powellimport android.view.accessibility.AccessibilityEvent; 3075d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powellimport android.widget.TextView; 317b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powellimport android.widget.Toast; 3296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 3396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell/** 3496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell * @hide 3596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell */ 3675d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powellpublic class ActionMenuItemView extends TextView 377b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener, 387b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell ActionMenuView.ActionMenuChildView { 3996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private static final String TAG = "ActionMenuItemView"; 40a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 4196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private MenuItemImpl mItemData; 4296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private CharSequence mTitle; 4375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell private Drawable mIcon; 4496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell private MenuBuilder.ItemInvoker mItemInvoker; 45a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 4635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell private boolean mAllowTextWithIcon; 4735aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell private boolean mExpandedFormat; 4824340afbab8ad4c191784fda8023407205bc0a75Adam Powell private int mMinWidth; 4975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell private int mSavedPaddingLeft; 501f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell 5196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public ActionMenuItemView(Context context) { 5296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell this(context, null); 5396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 54a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 5596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public ActionMenuItemView(Context context, AttributeSet attrs) { 5635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell this(context, attrs, 0); 5796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 58a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 5996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) { 6096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell super(context, attrs, defStyle); 6135aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell final Resources res = context.getResources(); 6235aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell mAllowTextWithIcon = res.getBoolean( 6335aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); 6424340afbab8ad4c191784fda8023407205bc0a75Adam Powell TypedArray a = context.obtainStyledAttributes(attrs, 6524340afbab8ad4c191784fda8023407205bc0a75Adam Powell com.android.internal.R.styleable.ActionMenuItemView, 0, 0); 6624340afbab8ad4c191784fda8023407205bc0a75Adam Powell mMinWidth = a.getDimensionPixelSize( 6724340afbab8ad4c191784fda8023407205bc0a75Adam Powell com.android.internal.R.styleable.ActionMenuItemView_minWidth, 0); 6824340afbab8ad4c191784fda8023407205bc0a75Adam Powell a.recycle(); 6996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 706bddd8771d05889024778caa78fb1eaae68a0802Adam Powell setOnClickListener(this); 717b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell setOnLongClickListener(this); 7275d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell 73367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell mSavedPaddingLeft = -1; 74367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell } 75367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell 76367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell @Override 77367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell public void setPadding(int l, int t, int r, int b) { 78367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell mSavedPaddingLeft = l; 79367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell super.setPadding(l, t, r, b); 801f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell } 811f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell 8296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public MenuItemImpl getItemData() { 8396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell return mItemData; 8496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 8596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 8696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void initialize(MenuItemImpl itemData, int menuType) { 8796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell mItemData = itemData; 88a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 8996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setIcon(itemData.getIcon()); 90e7d468410b3a783560d5158a5798cef1b4b67702Adam Powell setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon 9196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setId(itemData.getItemId()); 92a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 9396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); 9496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell setEnabled(itemData.isEnabled()); 9596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 96a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 971f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell public void onClick(View v) { 981f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell if (mItemInvoker != null) { 991f9c7afc5a06576e327a4b1c12688202f53d9462Adam Powell mItemInvoker.invokeItem(mItemData); 10096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 10196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 102a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 10396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setItemInvoker(MenuBuilder.ItemInvoker invoker) { 10496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell mItemInvoker = invoker; 10596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 10696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 10796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public boolean prefersCondensedTitle() { 108e7d468410b3a783560d5158a5798cef1b4b67702Adam Powell return true; 10996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 11096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 11196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setCheckable(boolean checkable) { 11296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell // TODO Support checkable action items 11396675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 11496675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 11596675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setChecked(boolean checked) { 11696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell // TODO Support checkable action items 11796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 11896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 11935aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell public void setExpandedFormat(boolean expandedFormat) { 12035aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell if (mExpandedFormat != expandedFormat) { 12135aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell mExpandedFormat = expandedFormat; 12235aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell if (mItemData != null) { 12335aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell mItemData.actionFormatChanged(); 12435aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell } 12535aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell } 12635aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell } 12735aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell 128c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne private void updateTextButtonVisibility() { 12975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell boolean visible = !TextUtils.isEmpty(mTitle); 13075d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell visible &= mIcon == null || 13135aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat)); 13235aecd5884a5ccfe380903e39f30f468315e8f92Adam Powell 13375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell setText(visible ? mTitle : null); 134c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne } 135c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne 13696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setIcon(Drawable icon) { 13775d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell mIcon = icon; 13875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); 1399f125d341521efc8535e32e35a641be50d405c36Adam Powell 140c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne updateTextButtonVisibility(); 14196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 142c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne 143be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell public boolean hasText() { 14475d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell return !TextUtils.isEmpty(getText()); 145be4d68e7b238b8ee879de0481e39c40d3f1683b6Adam Powell } 14696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 14796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setShortcut(boolean showShortcut, char shortcutKey) { 14896675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell // Action buttons don't show text for shortcut keys. 14996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 15096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 15196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public void setTitle(CharSequence title) { 15296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell mTitle = title; 153a8a72c38fec3879f5d346840b0d186d5903931b8Jeff Sharkey 1547bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell setContentDescription(mTitle); 155c619e74cc62678cef09b5f92e87e762a1f02aab7Gilles Debunne updateTextButtonVisibility(); 15696675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 15796675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell 1587bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell @Override 1597bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 1607bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell onPopulateAccessibilityEvent(event); 1617bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell return true; 1627bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 1637bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell 1647bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell @Override 1657bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 1667bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell super.onPopulateAccessibilityEvent(event); 1677bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell final CharSequence cdesc = getContentDescription(); 1687bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell if (!TextUtils.isEmpty(cdesc)) { 1697bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell event.getText().add(cdesc); 1707bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 1717bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 1727bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell 1737bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell @Override 1747bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell public boolean dispatchHoverEvent(MotionEvent event) { 1757bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell // Don't allow children to hover; we want this to be treated as a single component. 1767bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell return onHoverEvent(event); 1777bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell } 1787bc3ca0dc52be52ecad1c0de8c62a6a4bf8141caAdam Powell 17996675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell public boolean showsIcon() { 18096675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell return true; 18196675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell } 182696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 183696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public boolean needsDividerBefore() { 184696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell return hasText() && mItemData.getIcon() == null; 185696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell } 186696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell 187696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell public boolean needsDividerAfter() { 188696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell return hasText(); 189696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell } 1907b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell 1917b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell @Override 1927b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell public boolean onLongClick(View v) { 1937b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell if (hasText()) { 1947b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell // Don't show the cheat sheet for items that already show text. 1957b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell return false; 1967b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } 1977b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell 1987b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int[] screenPos = new int[2]; 1997b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final Rect displayFrame = new Rect(); 2007b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell getLocationOnScreen(screenPos); 2017b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell getWindowVisibleDisplayFrame(displayFrame); 2027b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell 2037b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final Context context = getContext(); 2047b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int width = getWidth(); 2057b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int height = getHeight(); 2067b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int midy = screenPos[1] + height / 2; 2077b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; 2087b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell 2097b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT); 2107b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell if (midy < displayFrame.height()) { 2117b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell // Show along the top; follow action buttons 2127b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT, 2137b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell screenWidth - screenPos[0] - width / 2, height); 2147b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } else { 2157b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell // Show along the bottom center 2167b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height); 2177b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } 2187b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell cheatSheet.show(); 2197b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell return true; 2207b5e9e6c1c7098b6d7b2c6f9ab0c316f0f66109eAdam Powell } 22124340afbab8ad4c191784fda8023407205bc0a75Adam Powell 22224340afbab8ad4c191784fda8023407205bc0a75Adam Powell @Override 22324340afbab8ad4c191784fda8023407205bc0a75Adam Powell protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 22475d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final boolean textVisible = hasText(); 225367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell if (textVisible && mSavedPaddingLeft >= 0) { 226367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell super.setPadding(mSavedPaddingLeft, getPaddingTop(), 227367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell getPaddingRight(), getPaddingBottom()); 22875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell } 22975d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell 23024340afbab8ad4c191784fda8023407205bc0a75Adam Powell super.onMeasure(widthMeasureSpec, heightMeasureSpec); 23124340afbab8ad4c191784fda8023407205bc0a75Adam Powell 23224340afbab8ad4c191784fda8023407205bc0a75Adam Powell final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 23375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final int widthSize = MeasureSpec.getSize(widthMeasureSpec); 23424340afbab8ad4c191784fda8023407205bc0a75Adam Powell final int oldMeasuredWidth = getMeasuredWidth(); 23575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth) 23624340afbab8ad4c191784fda8023407205bc0a75Adam Powell : mMinWidth; 23724340afbab8ad4c191784fda8023407205bc0a75Adam Powell 23824340afbab8ad4c191784fda8023407205bc0a75Adam Powell if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) { 23924340afbab8ad4c191784fda8023407205bc0a75Adam Powell // Remeasure at exactly the minimum width. 24024340afbab8ad4c191784fda8023407205bc0a75Adam Powell super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), 24124340afbab8ad4c191784fda8023407205bc0a75Adam Powell heightMeasureSpec); 24224340afbab8ad4c191784fda8023407205bc0a75Adam Powell } 24375d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell 24475d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell if (!textVisible && mIcon != null) { 24575d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell // TextView won't center compound drawables in both dimensions without 24675d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell // a little coercion. Pad in to center the icon after we've measured. 24775d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final int w = getMeasuredWidth(); 24875d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell final int dw = mIcon.getIntrinsicWidth(); 249367ee326058bbee6fc179b8b1eb2174fe7ba8f45Adam Powell super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom()); 25075d022af1f24cf2d8a7551183ea5bbe943d25d21Adam Powell } 25124340afbab8ad4c191784fda8023407205bc0a75Adam Powell } 25296675b1df3969f2d313b68f60ed9fa36805db8ceAdam Powell} 253