ActionMenuItemView.java revision 30837f1095c803f332f4a1c3f0917c8afdd50156
1bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/*
2bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Copyright (C) 2010 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
17da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownpackage android.support.v7.internal.view.menu;
18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
19bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Context;
20bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.res.Resources;
21bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.res.TypedArray;
22bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.graphics.Rect;
23bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.graphics.drawable.Drawable;
24da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownimport android.support.v7.appcompat.R;
25bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.text.TextUtils;
2630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.text.method.TransformationMethod;
27bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.util.AttributeSet;
28bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.Gravity;
29bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.View;
30bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.TextView;
31bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.Toast;
32bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport java.util.Locale;
3430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
35bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/**
36bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * @hide
37bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
38bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellpublic class ActionMenuItemView extends TextView
3920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener,
4020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        ActionMenuView.ActionMenuChildView {
4120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns
42ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final String TAG = "ActionMenuItemView";
43ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
44ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private MenuItemImpl mItemData;
45ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private CharSequence mTitle;
46ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Drawable mIcon;
47ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private MenuBuilder.ItemInvoker mItemInvoker;
48ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
49ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mAllowTextWithIcon;
50ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mExpandedFormat;
51ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mMinWidth;
52ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mSavedPaddingLeft;
53ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
54ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ActionMenuItemView(Context context) {
55ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        this(context, null);
56ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
57ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
58ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ActionMenuItemView(Context context, AttributeSet attrs) {
59ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        this(context, attrs, 0);
60ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
61ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
62ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
63ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super(context, attrs, defStyle);
64ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final Resources res = context.getResources();
65ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mAllowTextWithIcon = res.getBoolean(
66da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brown                android.support.v7.appcompat.R.bool.config_allowActionMenuItemTextWithIcon);
67ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        TypedArray a = context.obtainStyledAttributes(attrs,
68da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brown                android.support.v7.appcompat.R.styleable.ActionMenuItemView, 0, 0);
69ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mMinWidth = a.getDimensionPixelSize(
7020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                R.styleable.ActionMenuItemView_android_minWidth, 0);
71ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        a.recycle();
72ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
73ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setOnClickListener(this);
74ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setOnLongClickListener(this);
75ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
7630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        setTransformationMethod(new AllCapsTransformationMethod());
7730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
78ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mSavedPaddingLeft = -1;
79ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
80ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
81ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
82ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setPadding(int l, int t, int r, int b) {
83ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mSavedPaddingLeft = l;
84ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.setPadding(l, t, r, b);
85ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
86ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
87ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItemImpl getItemData() {
88ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mItemData;
89ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
90ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
91ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void initialize(MenuItemImpl itemData, int menuType) {
92ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mItemData = itemData;
93ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
94ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setIcon(itemData.getIcon());
95ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon
96ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setId(itemData.getItemId());
97ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
98ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
99ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setEnabled(itemData.isEnabled());
100ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
101ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
102ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void onClick(View v) {
103ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemInvoker != null) {
104ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mItemInvoker.invokeItem(mItemData);
105ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
106ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
107ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
108ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
10920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        mItemInvoker = invoker;
110ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
111ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
112ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean prefersCondensedTitle() {
113ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return true;
114ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
115ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
116ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setCheckable(boolean checkable) {
117ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // TODO Support checkable action items
118ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
119ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
120ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setChecked(boolean checked) {
121ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // TODO Support checkable action items
122ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
123ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
124ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setExpandedFormat(boolean expandedFormat) {
125ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mExpandedFormat != expandedFormat) {
126ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mExpandedFormat = expandedFormat;
127ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mItemData != null) {
128ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                mItemData.actionFormatChanged();
129ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
130ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
131ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
132ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
133ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void updateTextButtonVisibility() {
134ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        boolean visible = !TextUtils.isEmpty(mTitle);
135ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        visible &= mIcon == null ||
13620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat));
137ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
138ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setText(visible ? mTitle : null);
139ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
140ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
141ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setIcon(Drawable icon) {
142ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIcon = icon;
143ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
144ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
145ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        updateTextButtonVisibility();
146ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
147ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
148ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean hasText() {
149ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return !TextUtils.isEmpty(getText());
150ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
151ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
152ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setShortcut(boolean showShortcut, char shortcutKey) {
153ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // Action buttons don't show text for shortcut keys.
154ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
155ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
156ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setTitle(CharSequence title) {
157ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTitle = title;
158ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
159ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setContentDescription(mTitle);
160ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        updateTextButtonVisibility();
161ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
162ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
163ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean showsIcon() {
164ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return true;
165ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
166ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
167ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean needsDividerBefore() {
168ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return hasText() && mItemData.getIcon() == null;
169ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
170ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
171ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean needsDividerAfter() {
172ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return hasText();
173ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
174ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
175ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
176ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean onLongClick(View v) {
177ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (hasText()) {
178ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Don't show the cheat sheet for items that already show text.
179ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return false;
180ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
181ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
182ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int[] screenPos = new int[2];
183ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final Rect displayFrame = new Rect();
184ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        getLocationOnScreen(screenPos);
185ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        getWindowVisibleDisplayFrame(displayFrame);
186ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
187ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final Context context = getContext();
188ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int width = getWidth();
189ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int height = getHeight();
190ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int midy = screenPos[1] + height / 2;
191ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
192ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
193ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);
194ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (midy < displayFrame.height()) {
195ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Show along the top; follow action buttons
196ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT,
19720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                    screenWidth - screenPos[0] - width / 2, height);
198ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
199ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Show along the bottom center
200ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
201ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
202ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        cheatSheet.show();
203ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return true;
204ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
205ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
206ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
207ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
208ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final boolean textVisible = hasText();
209ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (textVisible && mSavedPaddingLeft >= 0) {
210ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            super.setPadding(mSavedPaddingLeft, getPaddingTop(),
21120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                    getPaddingRight(), getPaddingBottom());
212ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
213ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
214ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
215ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
216ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
217ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
218ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int oldMeasuredWidth = getMeasuredWidth();
219ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth)
22020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                : mMinWidth;
221ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
222ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) {
223ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Remeasure at exactly the minimum width.
224ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY),
22520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                    heightMeasureSpec);
226ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
227ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
228ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (!textVisible && mIcon != null) {
229ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // TextView won't center compound drawables in both dimensions without
230ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // a little coercion. Pad in to center the icon after we've measured.
231ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            final int w = getMeasuredWidth();
232ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            final int dw = mIcon.getIntrinsicWidth();
233ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom());
234ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
235ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
23630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
23730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    private class AllCapsTransformationMethod implements TransformationMethod {
23830837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        private Locale mLocale;
23930837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
24030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        public AllCapsTransformationMethod() {
24130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell            mLocale = getContext().getResources().getConfiguration().locale;
24230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        }
24330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
24430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        @Override
24530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        public CharSequence getTransformation(CharSequence source, View view) {
24630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell            return source != null ? source.toString().toUpperCase(mLocale) : null;
24730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        }
24830837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
24930837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        @Override
25030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        public void onFocusChanged(View view, CharSequence sourceText, boolean focused,
25130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                int direction, Rect previouslyFocusedRect) {
25230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        }
25330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    }
254bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell}
255