Toolbar.java revision 77e5037d8125b94e524264dc1890ab9b6b7df0af
1e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell/*
2e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * Copyright (C) 2014 The Android Open Source Project
3e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
4e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * you may not use this file except in compliance with the License.
6e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * You may obtain a copy of the License at
7e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
8e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
10e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * Unless required by applicable law or agreed to in writing, software
11e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * See the License for the specific language governing permissions and
14e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * limitations under the License.
15e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell */
16e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
17e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellpackage android.widget;
18e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
19e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.annotation.NonNull;
20e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport android.app.ActionBar;
21e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.content.Context;
22e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.content.res.TypedArray;
23e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.graphics.drawable.Drawable;
24e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.os.Parcel;
25e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.os.Parcelable;
26e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport android.text.Layout;
27e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.text.TextUtils;
28e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.util.AttributeSet;
29e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport android.view.CollapsibleActionView;
303d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viveretteimport android.view.ContextThemeWrapper;
31e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.Gravity;
32e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.Menu;
33e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.MenuInflater;
34e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.MenuItem;
35e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.View;
36e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.ViewGroup;
373d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
38e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport com.android.internal.R;
39e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuBuilder;
40e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuItemImpl;
41e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuPresenter;
42e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuView;
43e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.SubMenuBuilder;
44e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.widget.DecorToolbar;
45e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.widget.ToolbarWidgetWrapper;
46e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
47e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport java.util.ArrayList;
48e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport java.util.List;
49e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
50e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell/**
51e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * A standard toolbar for use within application content.
52e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
53e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <p>A Toolbar is a generalization of {@link android.app.ActionBar action bars} for use
54e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * within application layouts. While an action bar is traditionally part of an
55e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * {@link android.app.Activity Activity's} opaque window decor controlled by the framework,
56e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy.
57e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * An application may choose to designate a Toolbar as the action bar for an Activity
58e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * using the {@link android.app.Activity#setActionBar(Toolbar) setActionBar()} method.</p>
59e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
60e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <p>Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar
61e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * may contain a combination of the following optional elements:
62e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
63e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <ul>
64e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>A navigation button.</em> This may be an Up arrow, navigation menu toggle, close,
65e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     collapse, done or another glyph of the app's choosing. This button should always be used
66e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     to access other navigational destinations within the container of the Toolbar and
67e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     its signified content or otherwise leave the current context signified by the Toolbar.</li>
68e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>A branded logo image.</em> This may extend to the height of the bar and can be
69e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     arbitrarily wide.</li>
70e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>A title and subtitle.</em> The title should be a signpost for the Toolbar's current
71e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     position in the navigation hierarchy and the content contained there. The subtitle,
72e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     if present should indicate any extended information about the current content.
73e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     If an app uses a logo image it should strongly consider omitting a title and subtitle.</li>
74e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>One or more custom views.</em> The application may add arbitrary child views
75e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     to the Toolbar. They will appear at this position within the layout. If a child view's
76e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     {@link LayoutParams} indicates a {@link Gravity} value of
77e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     {@link Gravity#CENTER_HORIZONTAL CENTER_HORIZONTAL} the view will attempt to center
78e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     within the available space remaining in the Toolbar after all other elements have been
79e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     measured.</li>
80e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>An {@link ActionMenuView action menu}.</em> The menu of actions will pin to the
81e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     end of the Toolbar offering a few
82e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <a href="http://developer.android.com/design/patterns/actionbar.html#ActionButtons">
83e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *         frequent, important or typical</a> actions along with an optional overflow menu for
84e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *         additional actions.</li>
85e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * </ul>
86e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * </p>
87e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
88e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <p>In modern Android UIs developers should lean more on a visually distinct color scheme for
89e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * toolbars than on their application icon. The use of application icon plus title as a standard
90e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * layout is discouraged on API 21 devices and newer.</p>
91e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell */
92e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellpublic class Toolbar extends ViewGroup {
93e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private static final String TAG = "Toolbar";
94e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
95e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private ActionMenuView mMenuView;
96e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private TextView mTitleTextView;
97e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private TextView mSubtitleTextView;
98e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private ImageButton mNavButtonView;
99e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private ImageView mLogoView;
100e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
101e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private Drawable mCollapseIcon;
102e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ImageButton mCollapseButtonView;
103e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    View mExpandedActionView;
104e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1053d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /** Context against which to inflate popup menus. */
1063d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    private Context mPopupContext;
1073d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
1083d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /** Theme resource against which to inflate popup menus. */
1093d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    private int mPopupTheme;
1103d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
111e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleTextAppearance;
112e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mSubtitleTextAppearance;
113e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private int mNavButtonStyle;
114e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
115e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private int mButtonGravity;
116e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
117e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private int mMaxButtonHeight;
11876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
119e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginStart;
120e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginEnd;
121e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginTop;
122e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginBottom;
123e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
12476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    private final RtlSpacingHelper mContentInsets = new RtlSpacingHelper();
12576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
126e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mGravity = Gravity.START | Gravity.CENTER_VERTICAL;
127e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
128e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private CharSequence mTitleText;
129e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private CharSequence mSubtitleText;
130e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1311a264ce3d915b187b71101436bec3328c76288b0Adam Powell    private int mTitleTextColor;
1321a264ce3d915b187b71101436bec3328c76288b0Adam Powell    private int mSubtitleTextColor;
1331a264ce3d915b187b71101436bec3328c76288b0Adam Powell
134e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    // Clear me after use.
135e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private final ArrayList<View> mTempViews = new ArrayList<View>();
136e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1377a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private final int[] mTempMargins = new int[2];
1387a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
139e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private OnMenuItemClickListener mOnMenuItemClickListener;
140e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
141e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private final ActionMenuView.OnMenuItemClickListener mMenuViewItemClickListener =
142e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            new ActionMenuView.OnMenuItemClickListener() {
143e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                @Override
144e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                public boolean onMenuItemClick(MenuItem item) {
145e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    if (mOnMenuItemClickListener != null) {
146e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        return mOnMenuItemClickListener.onMenuItemClick(item);
147e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
148e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    return false;
149e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
150e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            };
151e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
152e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ToolbarWidgetWrapper mWrapper;
153e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ActionMenuPresenter mOuterActionMenuPresenter;
154e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ExpandedActionViewMenuPresenter mExpandedMenuPresenter;
155e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
156360c1d89227570510de7bb11195c986300488464Adam Powell    private boolean mCollapsible;
157360c1d89227570510de7bb11195c986300488464Adam Powell
158e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context) {
159e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        this(context, null);
160e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
161e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
162e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context, AttributeSet attrs) {
163e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        this(context, attrs, com.android.internal.R.attr.toolbarStyle);
164e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
165e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
166e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context, AttributeSet attrs, int defStyleAttr) {
167e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        this(context, attrs, defStyleAttr, 0);
168e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
169e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
170e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
171e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        super(context, attrs, defStyleAttr, defStyleRes);
172e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
173e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Toolbar,
174e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                defStyleAttr, defStyleRes);
175e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
176e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTitleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
177e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mSubtitleTextAppearance = a.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, 0);
178e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mNavButtonStyle = a.getResourceId(R.styleable.Toolbar_navigationButtonStyle, 0);
179e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mGravity = a.getInteger(R.styleable.Toolbar_gravity, mGravity);
180e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);
18176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mTitleMarginStart = mTitleMarginEnd = mTitleMarginTop = mTitleMarginBottom =
18276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelOffset(R.styleable.Toolbar_titleMargins, 0);
183e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
184e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginStart = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginStart, -1);
185e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginStart >= 0) {
186e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginStart = marginStart;
187e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
188e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
189e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginEnd = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginEnd, -1);
190e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginEnd >= 0) {
191e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginEnd = marginEnd;
192e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
193e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
194e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginTop = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginTop, -1);
195e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginTop >= 0) {
196e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginTop = marginTop;
197e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
198e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
199e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginBottom = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginBottom,
200e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                -1);
201e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginBottom >= 0) {
202e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginBottom = marginBottom;
203e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
204e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
205e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mMaxButtonHeight = a.getDimensionPixelSize(R.styleable.Toolbar_maxButtonHeight, -1);
206e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
20776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetStart =
20876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetStart,
20976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                        RtlSpacingHelper.UNDEFINED);
21076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetEnd =
21176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetEnd,
21276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                        RtlSpacingHelper.UNDEFINED);
21376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetLeft =
21476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetLeft, 0);
21576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetRight =
21676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0);
21776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
21876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
21976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
22076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        if (contentInsetStart != RtlSpacingHelper.UNDEFINED ||
22176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                contentInsetEnd != RtlSpacingHelper.UNDEFINED) {
22276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
22376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        }
22476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
225e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mCollapseIcon = a.getDrawable(R.styleable.Toolbar_collapseIcon);
226e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
227e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final CharSequence title = a.getText(R.styleable.Toolbar_title);
228e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(title)) {
229e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            setTitle(title);
230e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
231e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
232e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final CharSequence subtitle = a.getText(R.styleable.Toolbar_subtitle);
233e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(subtitle)) {
23407a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            setSubtitle(subtitle);
235e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2363d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
23777e5037d8125b94e524264dc1890ab9b6b7df0afAlan Viverette        // Set the default context, since setPopupTheme() may be a no-op.
23877e5037d8125b94e524264dc1890ab9b6b7df0afAlan Viverette        mPopupContext = mContext;
23922a117d1b60869ed36d125796396f190b4b002fbAlan Viverette        setPopupTheme(a.getResourceId(R.styleable.Toolbar_popupTheme, 0));
24022a117d1b60869ed36d125796396f190b4b002fbAlan Viverette        a.recycle();
2413d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    }
2423d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
2433d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /**
2443d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * Specifies the theme to use when inflating popup menus. By default, uses
2453d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * the same theme as the toolbar itself.
2463d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     *
2473d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @param resId theme used to inflate popup menus
2483d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @see #getPopupTheme()
2493d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     */
2503d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    public void setPopupTheme(int resId) {
2513d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        if (mPopupTheme != resId) {
2523d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mPopupTheme = resId;
2533d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            if (resId == 0) {
2543d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette                mPopupContext = mContext;
2553d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            } else {
2563d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette                mPopupContext = new ContextThemeWrapper(mContext, resId);
2573d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            }
2583d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        }
2593d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    }
2603d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
2613d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /**
2623d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @return resource identifier of the theme used to inflate popup menus, or
2633d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     *         0 if menus are inflated against the toolbar theme
2643d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @see #setPopupTheme(int)
2653d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     */
2663d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    public int getPopupTheme() {
2673d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        return mPopupTheme;
268e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
269e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
27076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    @Override
27176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) {
27276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        super.onRtlPropertiesChanged(layoutDirection);
27376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setDirection(layoutDirection == LAYOUT_DIRECTION_RTL);
27476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
27576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
276e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
277e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a logo drawable from a resource id.
278e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
279e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This drawable should generally take the place of title text. The logo cannot be
280e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * clicked. Apps using a logo should also supply a description using
281e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setLogoDescription(int)}.</p>
282e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
283e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId ID of a drawable resource
284e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
285e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogo(int resId) {
286e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setLogo(getContext().getDrawable(resId));
287e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
288e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
289e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
290e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean canShowOverflowMenu() {
291e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return getVisibility() == VISIBLE && mMenuView != null && mMenuView.isOverflowReserved();
292e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
293e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
294e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
295e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Check whether the overflow menu is currently showing. This may not reflect
296e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * a pending show operation in progress.
297e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
298e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the overflow menu is currently showing
299e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
300e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isOverflowMenuShowing() {
301e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.isOverflowMenuShowing();
302e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
303e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
304e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
305e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isOverflowMenuShowPending() {
306e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.isOverflowMenuShowPending();
307e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
308e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
309e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
310e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Show the overflow items from the associated menu.
311e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
312e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the menu was able to be shown, false otherwise
313e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
314e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean showOverflowMenu() {
315e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.showOverflowMenu();
316e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
317e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
318e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
319e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Hide the overflow items from the associated menu.
320e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
321e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the menu was able to be hidden, false otherwise
322e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
323e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean hideOverflowMenu() {
324e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.hideOverflowMenu();
325e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
326e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
327e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
328e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void setMenu(MenuBuilder menu, ActionMenuPresenter outerPresenter) {
329e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (menu == null && mMenuView == null) {
330e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return;
331e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
332e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
333e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        ensureMenuView();
334e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MenuBuilder oldMenu = mMenuView.peekMenu();
335e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (oldMenu == menu) {
336e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return;
337e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
338e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
339e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (oldMenu != null) {
340e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            oldMenu.removeMenuPresenter(mOuterActionMenuPresenter);
341e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            oldMenu.removeMenuPresenter(mExpandedMenuPresenter);
342e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
343e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
344e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mExpandedMenuPresenter == null) {
345e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
346e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
347e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
348e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        outerPresenter.setExpandedActionViewsExclusive(true);
349e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (menu != null) {
3503d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(outerPresenter, mPopupContext);
3513d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(mExpandedMenuPresenter, mPopupContext);
352e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        } else {
3533d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            outerPresenter.initForMenu(mPopupContext, null);
3543d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mExpandedMenuPresenter.initForMenu(mPopupContext, null);
355e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            outerPresenter.updateMenuView(true);
356e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedMenuPresenter.updateMenuView(true);
357e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
3583d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        mMenuView.setPopupTheme(mPopupTheme);
359e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mMenuView.setPresenter(outerPresenter);
360e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mOuterActionMenuPresenter = outerPresenter;
361e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
362e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
363e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
364e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Dismiss all currently showing popup menus, including overflow or submenus.
365e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
366e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void dismissPopupMenus() {
367e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mMenuView != null) {
368e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenuView.dismissPopupMenus();
369e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
370e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
371e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
372e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
373e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isTitleTruncated() {
374e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mTitleTextView == null) {
375e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
376e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
377e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
378e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final Layout titleLayout = mTitleTextView.getLayout();
379e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (titleLayout == null) {
380e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
381e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
382e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
383e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int lineCount = titleLayout.getLineCount();
384e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        for (int i = 0; i < lineCount; i++) {
385e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (titleLayout.getEllipsisCount(i) > 0) {
386e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                return true;
387e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
388e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
389e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return false;
390e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
391e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
392e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
393e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a logo drawable.
394e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
395e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This drawable should generally take the place of title text. The logo cannot be
396e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * clicked. Apps using a logo should also supply a description using
397e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setLogoDescription(int)}.</p>
398e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
399e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param drawable Drawable to use as a logo
400e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
401e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogo(Drawable drawable) {
402e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (drawable != null) {
403e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureLogoView();
404e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mLogoView.getParent() == null) {
405e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mLogoView);
406e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
407e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mLogoView != null && mLogoView.getParent() != null) {
408e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mLogoView);
409e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
410e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mLogoView != null) {
411e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mLogoView.setImageDrawable(drawable);
412e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
413e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
414e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
415e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
416e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the current logo drawable.
417e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
418e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The current logo drawable
419e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setLogo(int)
420e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setLogo(android.graphics.drawable.Drawable)
421e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
422e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Drawable getLogo() {
423e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mLogoView != null ? mLogoView.getDrawable() : null;
424e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
425e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
426e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
427e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description of the toolbar's logo.
428e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
429e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description will be used for accessibility or other similar descriptions
430e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * of the UI.</p>
431e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
432e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId String resource id
433e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
434e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogoDescription(int resId) {
435e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setLogoDescription(getContext().getText(resId));
436e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
437e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
438e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
439e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description of the toolbar's logo.
440e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
441e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description will be used for accessibility or other similar descriptions
442e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * of the UI.</p>
443e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
444e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param description Description to set
445e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
446e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogoDescription(CharSequence description) {
447e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (!TextUtils.isEmpty(description)) {
448e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureLogoView();
449e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
450e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mLogoView != null) {
451e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mLogoView.setContentDescription(description);
452e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
453e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
454e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
455e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
456e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the description of the toolbar's logo.
457e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
458e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return A description of the logo
459e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
460e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getLogoDescription() {
461e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mLogoView != null ? mLogoView.getContentDescription() : null;
462e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
463e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
464e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureLogoView() {
465e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mLogoView == null) {
466e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mLogoView = new ImageView(getContext());
467e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
468e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
469e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
470e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
471e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Check whether this Toolbar is currently hosting an expanded action view.
472e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
473e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * <p>An action view may be expanded either directly from the
474e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * {@link android.view.MenuItem MenuItem} it belongs to or by user action. If the Toolbar
475e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * has an expanded action view it can be collapsed using the {@link #collapseActionView()}
476e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * method.</p>
477e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
478e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the Toolbar has an expanded action view
479e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
480e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean hasExpandedActionView() {
481e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mExpandedMenuPresenter != null &&
482e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedMenuPresenter.mCurrentExpandedItem != null;
483e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
484e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
485e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
486e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Collapse a currently expanded action view. If this Toolbar does not have an
487e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * expanded action view this method has no effect.
488e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
489e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * <p>An action view may be expanded either directly from the
490e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * {@link android.view.MenuItem MenuItem} it belongs to or by user action.</p>
491e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
492e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @see #hasExpandedActionView()
493e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
494e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void collapseActionView() {
495e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MenuItemImpl item = mExpandedMenuPresenter == null ? null :
496e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedMenuPresenter.mCurrentExpandedItem;
497e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (item != null) {
498e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.collapseActionView();
499e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
500e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
501e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
502e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
503e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Returns the title of this toolbar.
504e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
505e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return The current title.
506e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
507e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getTitle() {
508e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mTitleText;
509e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
510e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
511e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
512e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the title of this toolbar.
513e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
514e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>A title should be used as the anchor for a section of content. It should
515e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * describe or name the content being viewed.</p>
516e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
517e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId Resource ID of a string to set as the title
518e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
519e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setTitle(int resId) {
520e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setTitle(getContext().getText(resId));
521e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
522e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
523e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
524e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the title of this toolbar.
525e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
526e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>A title should be used as the anchor for a section of content. It should
527e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * describe or name the content being viewed.</p>
528e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
529e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param title Title to set
530e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
531e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setTitle(CharSequence title) {
532e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(title)) {
533e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mTitleTextView == null) {
534e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final Context context = getContext();
535e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                mTitleTextView = new TextView(context);
536e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mTitleTextView.setSingleLine();
537e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
5381a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mTitleTextAppearance != 0) {
5391a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mTitleTextView.setTextAppearance(context, mTitleTextAppearance);
5401a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
5411a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mTitleTextColor != 0) {
5421a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mTitleTextView.setTextColor(mTitleTextColor);
5431a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
544e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
545e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mTitleTextView.getParent() == null) {
546e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mTitleTextView);
547e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
548e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mTitleTextView != null && mTitleTextView.getParent() != null) {
549e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mTitleTextView);
550e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
551e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mTitleTextView != null) {
552e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleTextView.setText(title);
553e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
554e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTitleText = title;
555e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
556e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
557e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
558e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the subtitle of this toolbar.
559e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
560e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The current subtitle
561e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
562e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getSubtitle() {
563e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mSubtitleText;
564e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
565e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
566e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
567e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the subtitle of this toolbar.
568e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
569e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Subtitles should express extended information about the current content.</p>
570e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
571e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId String resource ID
572e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
573e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setSubtitle(int resId) {
574e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setSubtitle(getContext().getText(resId));
575e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
576e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
577e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
578e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the subtitle of this toolbar.
579e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
580e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Subtitles should express extended information about the current content.</p>
581e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
582e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param subtitle Subtitle to set
583e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
584e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setSubtitle(CharSequence subtitle) {
585e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(subtitle)) {
586e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mSubtitleTextView == null) {
587e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final Context context = getContext();
588e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                mSubtitleTextView = new TextView(context);
589e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mSubtitleTextView.setSingleLine();
590e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mSubtitleTextView.setEllipsize(TextUtils.TruncateAt.END);
5911a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mSubtitleTextAppearance != 0) {
5921a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mSubtitleTextView.setTextAppearance(context, mSubtitleTextAppearance);
5931a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
5941a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mSubtitleTextColor != 0) {
5951a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mSubtitleTextView.setTextColor(mSubtitleTextColor);
5961a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
597e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
598e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mSubtitleTextView.getParent() == null) {
599e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mSubtitleTextView);
600e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
601e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mSubtitleTextView != null && mSubtitleTextView.getParent() != null) {
602e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mSubtitleTextView);
603e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
604e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mSubtitleTextView != null) {
605e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mSubtitleTextView.setText(subtitle);
606e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
607e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mSubtitleText = subtitle;
608e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
609e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
610e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
611ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * Sets the text color, size, style, hint color, and highlight color
612ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * from the specified TextAppearance resource.
613ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     */
614ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    public void setTitleTextAppearance(Context context, int resId) {
615ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        mTitleTextAppearance = resId;
616ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        if (mTitleTextView != null) {
617ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell            mTitleTextView.setTextAppearance(context, resId);
618ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        }
619ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    }
620ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell
621ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    /**
622ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * Sets the text color, size, style, hint color, and highlight color
623ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * from the specified TextAppearance resource.
624ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     */
625ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    public void setSubtitleTextAppearance(Context context, int resId) {
626ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        mSubtitleTextAppearance = resId;
627ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        if (mSubtitleTextView != null) {
628ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell            mSubtitleTextView.setTextAppearance(context, resId);
629ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        }
630ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    }
631ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell
632ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    /**
6331a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * Sets the text color of the title, if present.
6341a264ce3d915b187b71101436bec3328c76288b0Adam Powell     *
6351a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * @param color The new text color in 0xAARRGGBB format
6361a264ce3d915b187b71101436bec3328c76288b0Adam Powell     */
6371a264ce3d915b187b71101436bec3328c76288b0Adam Powell    public void setTitleTextColor(int color) {
6381a264ce3d915b187b71101436bec3328c76288b0Adam Powell        mTitleTextColor = color;
6391a264ce3d915b187b71101436bec3328c76288b0Adam Powell        if (mTitleTextView != null) {
6401a264ce3d915b187b71101436bec3328c76288b0Adam Powell            mTitleTextView.setTextColor(color);
6411a264ce3d915b187b71101436bec3328c76288b0Adam Powell        }
6421a264ce3d915b187b71101436bec3328c76288b0Adam Powell    }
6431a264ce3d915b187b71101436bec3328c76288b0Adam Powell
6441a264ce3d915b187b71101436bec3328c76288b0Adam Powell    /**
6451a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * Sets the text color of the subtitle, if present.
6461a264ce3d915b187b71101436bec3328c76288b0Adam Powell     *
6471a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * @param color The new text color in 0xAARRGGBB format
6481a264ce3d915b187b71101436bec3328c76288b0Adam Powell     */
6491a264ce3d915b187b71101436bec3328c76288b0Adam Powell    public void setSubtitleTextColor(int color) {
6501a264ce3d915b187b71101436bec3328c76288b0Adam Powell        mSubtitleTextColor = color;
6511a264ce3d915b187b71101436bec3328c76288b0Adam Powell        if (mSubtitleTextView != null) {
6521a264ce3d915b187b71101436bec3328c76288b0Adam Powell            mSubtitleTextView.setTextColor(color);
6531a264ce3d915b187b71101436bec3328c76288b0Adam Powell        }
6541a264ce3d915b187b71101436bec3328c76288b0Adam Powell    }
6551a264ce3d915b187b71101436bec3328c76288b0Adam Powell
6561a264ce3d915b187b71101436bec3328c76288b0Adam Powell    /**
657e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the icon to use for the toolbar's navigation button.
658e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
659e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>The navigation button appears at the start of the toolbar if present. Setting an icon
660e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * will make the navigation button visible.</p>
661e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
662e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>If you use a navigation icon you should also set a description for its action using
663e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setNavigationDescription(int)}. This is used for accessibility and tooltips.</p>
664e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
665e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId Resource ID of a drawable to set
666e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
667e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationIcon(int resId) {
668e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setNavigationIcon(getContext().getDrawable(resId));
669e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
670e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
671e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
6727901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * Retrieve the currently configured content description for the navigation button view.
6737901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * This will be used to describe the navigation action to users through mechanisms such
6747901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * as screen readers or tooltips.
6757901b2a54981b769bf27852c41ce612871a8e019Adam Powell     *
6767901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * @return The navigation button's content description
6777901b2a54981b769bf27852c41ce612871a8e019Adam Powell     */
6787901b2a54981b769bf27852c41ce612871a8e019Adam Powell    public CharSequence getNavigationContentDescription() {
6797901b2a54981b769bf27852c41ce612871a8e019Adam Powell        return mNavButtonView != null ? mNavButtonView.getContentDescription() : null;
6807901b2a54981b769bf27852c41ce612871a8e019Adam Powell    }
6817901b2a54981b769bf27852c41ce612871a8e019Adam Powell
6827901b2a54981b769bf27852c41ce612871a8e019Adam Powell    /**
683e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Set a content description for the navigation button if one is present. The content
684e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * description will be read via screen readers or other accessibility systems to explain
685e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * the action of the navigation button.
686e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
687e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @param description Content description to set
688e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
689e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void setNavigationContentDescription(CharSequence description) {
690e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        ensureNavButtonView();
691e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mNavButtonView.setContentDescription(description);
692e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
693e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
694e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
695e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Set a content description for the navigation button if one is present. The content
696e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * description will be read via screen readers or other accessibility systems to explain
697e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * the action of the navigation button.
698e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
699e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @param resId Resource ID of a content description string to set
700e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
701e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void setNavigationContentDescription(int resId) {
702e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        ensureNavButtonView();
7038e5372fe93ee2ee3bf20baab9054b39b4c5043ceAdam Powell        mNavButtonView.setContentDescription(resId != 0 ? getContext().getText(resId) : null);
704e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
705e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
706e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
707e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the icon to use for the toolbar's navigation button.
708e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
709e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>The navigation button appears at the start of the toolbar if present. Setting an icon
710e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * will make the navigation button visible.</p>
711e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
712e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>If you use a navigation icon you should also set a description for its action using
713e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setNavigationDescription(int)}. This is used for accessibility and tooltips.</p>
714e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
715e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param icon Drawable to set
716e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
717e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationIcon(Drawable icon) {
718e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (icon != null) {
719e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            ensureNavButtonView();
720e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mNavButtonView.getParent() == null) {
721e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mNavButtonView);
722e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
723e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mNavButtonView != null && mNavButtonView.getParent() != null) {
724e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mNavButtonView);
725e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
726e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView != null) {
727e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mNavButtonView.setImageDrawable(icon);
728e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
729e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
730e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
731e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
732e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the current drawable used as the navigation icon.
733e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
734e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The navigation icon drawable
735e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
736e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Drawable getNavigationIcon() {
737e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mNavButtonView != null ? mNavButtonView.getDrawable() : null;
738e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
739e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
740e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
741e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description for the navigation button.
742e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
743e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description string is used for accessibility, tooltips and other facilities
744e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * to improve discoverability.</p>
745e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
746e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId Resource ID of a string to set
747e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
748e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationDescription(int resId) {
749e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setNavigationDescription(getContext().getText(resId));
750e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
751e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
752e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
753e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description for the navigation button.
754e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
755e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description string is used for accessibility, tooltips and other facilities
756e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * to improve discoverability.</p>
757e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
758e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param description String to set as the description
759e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
760e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationDescription(CharSequence description) {
761e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(description)) {
762e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            ensureNavButtonView();
763e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
764e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView != null) {
765e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mNavButtonView.setContentDescription(description);
766e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
767e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
768e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
769e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
770e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a listener to respond to navigation events.
771e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
772e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This listener will be called whenever the user clicks the navigation button
773e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * at the start of the toolbar. An icon must be set for the navigation button to appear.</p>
774e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
775e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param listener Listener to set
776e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setNavigationIcon(android.graphics.drawable.Drawable)
777e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
778e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationOnClickListener(OnClickListener listener) {
779e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        ensureNavButtonView();
780e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mNavButtonView.setOnClickListener(listener);
781e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
782e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
783e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
784e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the Menu shown in the toolbar.
785e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
786e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Applications that wish to populate the toolbar's menu can do so from here. To use
787e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * an XML menu resource, use {@link #inflateMenu(int)}.</p>
788e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
789e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The toolbar's Menu
790e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
791e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Menu getMenu() {
79207a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        ensureMenu();
793e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView.getMenu();
794e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
795e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
79607a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell    private void ensureMenu() {
79707a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        ensureMenuView();
79807a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        if (mMenuView.peekMenu() == null) {
79907a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            // Initialize a new menu for the first time.
80007a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            final MenuBuilder menu = (MenuBuilder) mMenuView.getMenu();
80107a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            if (mExpandedMenuPresenter == null) {
80207a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell                mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
80307a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            }
80407a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            mMenuView.setExpandedActionViewsExclusive(true);
8053d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(mExpandedMenuPresenter, mPopupContext);
80607a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        }
80707a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell    }
80807a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell
809e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureMenuView() {
810e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mMenuView == null) {
811e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mMenuView = new ActionMenuView(getContext());
8123d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mMenuView.setPopupTheme(mPopupTheme);
813e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mMenuView.setOnMenuItemClickListener(mMenuViewItemClickListener);
814e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
815e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.END | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
816e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenuView.setLayoutParams(lp);
817e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            addSystemView(mMenuView);
818e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
819e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
820e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
821e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private MenuInflater getMenuInflater() {
822e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return new MenuInflater(getContext());
823e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
824e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
825e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
826e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Inflate a menu resource into this toolbar.
827e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
828e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Inflate an XML menu resource into this toolbar. Existing items in the menu will not
829e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * be modified or removed.</p>
830e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
831e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId ID of a menu resource to inflate
832e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
833e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void inflateMenu(int resId) {
834e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        getMenuInflater().inflate(resId, getMenu());
835e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
836e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
837e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
838e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a listener to respond to menu item click events.
839e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
840e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This listener will be invoked whenever a user selects a menu item from
841e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * the action buttons presented at the end of the toolbar or the associated overflow.</p>
842e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
843e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param listener Listener to set
844e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
845e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
846e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mOnMenuItemClickListener = listener;
847e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
848e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
84976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
85076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Set the content insets for this toolbar relative to layout direction.
85176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
85276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
85376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
85476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
85576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
85676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetStart Content inset for the toolbar starting edge
85776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetEnd Content inset for the toolbar ending edge
85876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
85976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
86076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
86176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
86276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
86376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
86476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
86576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) {
86676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
86776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
86876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
86976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
87076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the starting content inset for this toolbar.
87176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
87276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
87376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
87476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
87576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
87676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The starting content inset for this toolbar
87776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
87876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
87976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
88076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
88176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
88276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
88376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
88476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetStart() {
88576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getStart();
88676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
88776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
88876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
88976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the ending content inset for this toolbar.
89076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
89176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
89276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
89376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
89476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
89576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The ending content inset for this toolbar
89676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
89776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
89876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
89976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
90076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
90176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
90276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
90376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetEnd() {
90476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getEnd();
90576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
90676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
90776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
90876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Set the content insets for this toolbar.
90976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
91076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
91176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
91276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
91376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
91476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetLeft Content inset for the toolbar's left edge
91576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetRight Content inset for the toolbar's right edge
91676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
91776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
91876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
91976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
92076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
92176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
92276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
92376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) {
92476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
92576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
92676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
92776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
92876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the left content inset for this toolbar.
92976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
93076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
93176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
93276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
93376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
93476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The left content inset for this toolbar
93576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
93676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
93776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
93876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
93976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
94076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
94176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
94276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetLeft() {
94376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getLeft();
94476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
94576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
94676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
94776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the right content inset for this toolbar.
94876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
94976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
95076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
95176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
95276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
95376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The right content inset for this toolbar
95476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
95576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
95676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
95776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
95876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
95976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
96076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
96176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetRight() {
96276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getRight();
96376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
96476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
965e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void ensureNavButtonView() {
966e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView == null) {
967e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mNavButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
968e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
969e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
970e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mNavButtonView.setLayoutParams(lp);
971e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
972e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
973e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
974e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureCollapseButtonView() {
975e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mCollapseButtonView == null) {
976e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
977e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setImageDrawable(mCollapseIcon);
978e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
979e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
980e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.mViewType = LayoutParams.EXPANDED;
981e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setLayoutParams(lp);
982e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setOnClickListener(new OnClickListener() {
983e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                @Override
984e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                public void onClick(View v) {
985e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    collapseActionView();
986e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
987e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            });
988e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
989e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
990e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
991e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void addSystemView(View v) {
992e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
993e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                LayoutParams.WRAP_CONTENT);
994e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        lp.mViewType = LayoutParams.SYSTEM;
995e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addView(v, lp);
996e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
997e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
998e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
999e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected Parcelable onSaveInstanceState() {
1000e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        SavedState state = new SavedState(super.onSaveInstanceState());
1001e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return state;
1002e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1003e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1004e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1005e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onRestoreInstanceState(Parcelable state) {
1006e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final SavedState ss = (SavedState) state;
1007e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        super.onRestoreInstanceState(ss.getSuperState());
1008e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1009e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1010eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    /**
1011eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell     * @hide
1012eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell     */
1013eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    @Override
1014eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    protected void onSetLayoutParams(View child, ViewGroup.LayoutParams lp) {
1015eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        /*
1016eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * Apps may set ActionBar.LayoutParams on their action bar custom views when
1017eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * a Toolbar is actually acting in the role of the action bar. Perform a quick
1018eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * switch with Toolbar.LayoutParams whenever this happens. This does leave open
1019eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * one potential gotcha: if an app retains the ActionBar.LayoutParams reference
1020eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * and attempts to keep making changes to it before layout those changes won't
1021eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * be reflected in the final results.
1022eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         */
1023eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        if (!checkLayoutParams(lp)) {
1024eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell            child.setLayoutParams(generateLayoutParams(lp));
1025eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        }
1026eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    }
1027eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell
1028e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void measureChildConstrained(View child, int parentWidthSpec, int widthUsed,
1029e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            int parentHeightSpec, int heightUsed, int heightConstraint) {
1030e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
1031e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1032e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        int childWidthSpec = getChildMeasureSpec(parentWidthSpec,
1033e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
1034e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        + widthUsed, lp.width);
1035e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        int childHeightSpec = getChildMeasureSpec(parentHeightSpec,
1036e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
1037e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        + heightUsed, lp.height);
1038e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1039e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int childHeightMode = MeasureSpec.getMode(childHeightSpec);
1040e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (childHeightMode != MeasureSpec.EXACTLY && heightConstraint >= 0) {
1041e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final int size = childHeightMode != MeasureSpec.UNSPECIFIED ?
1042e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    Math.min(MeasureSpec.getSize(childHeightSpec), heightConstraint) :
1043e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    heightConstraint;
1044e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childHeightSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
1045e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1046e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        child.measure(childWidthSpec, childHeightSpec);
1047e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1048e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
10497a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    /**
10507a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell     * Returns the width + uncollapsed margins
10517a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell     */
10527a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int measureChildCollapseMargins(View child,
10537a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            int parentWidthMeasureSpec, int widthUsed,
10547a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            int parentHeightMeasureSpec, int heightUsed, int[] collapsingMargins) {
10557a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
10567a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
10577a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int leftDiff = lp.leftMargin - collapsingMargins[0];
10587a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int rightDiff = lp.rightMargin - collapsingMargins[1];
10597a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int leftMargin = Math.max(0, leftDiff);
10607a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int rightMargin = Math.max(0, rightDiff);
10617a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int hMargins = leftMargin + rightMargin;
10627a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, -leftDiff);
10637a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, -rightDiff);
10647a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
10657a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
10667a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                mPaddingLeft + mPaddingRight + hMargins + widthUsed, lp.width);
10677a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
10687a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
10697a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                        + heightUsed, lp.height);
10707a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
10717a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
10727a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        return child.getMeasuredWidth() + hMargins;
10737a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    }
10747a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1075360c1d89227570510de7bb11195c986300488464Adam Powell    /**
1076360c1d89227570510de7bb11195c986300488464Adam Powell     * Returns true if the Toolbar is collapsible and has no child views with a measured size > 0.
1077360c1d89227570510de7bb11195c986300488464Adam Powell     */
1078360c1d89227570510de7bb11195c986300488464Adam Powell    private boolean shouldCollapse() {
1079360c1d89227570510de7bb11195c986300488464Adam Powell        if (!mCollapsible) return false;
1080360c1d89227570510de7bb11195c986300488464Adam Powell
1081360c1d89227570510de7bb11195c986300488464Adam Powell        final int childCount = getChildCount();
1082360c1d89227570510de7bb11195c986300488464Adam Powell        for (int i = 0; i < childCount; i++) {
1083360c1d89227570510de7bb11195c986300488464Adam Powell            final View child = getChildAt(i);
1084360c1d89227570510de7bb11195c986300488464Adam Powell            if (shouldLayout(child) && child.getMeasuredWidth() > 0 &&
1085360c1d89227570510de7bb11195c986300488464Adam Powell                    child.getMeasuredHeight() > 0) {
1086360c1d89227570510de7bb11195c986300488464Adam Powell                return false;
1087360c1d89227570510de7bb11195c986300488464Adam Powell            }
1088360c1d89227570510de7bb11195c986300488464Adam Powell        }
1089360c1d89227570510de7bb11195c986300488464Adam Powell        return true;
1090360c1d89227570510de7bb11195c986300488464Adam Powell    }
1091360c1d89227570510de7bb11195c986300488464Adam Powell
1092e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1093e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1094e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int width = 0;
1095e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int height = 0;
1096e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int childState = 0;
1097e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
10987a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int[] collapsingMargins = mTempMargins;
10997a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int marginStartIndex;
11007a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int marginEndIndex;
11017a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        if (isLayoutRtl()) {
11027a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginStartIndex = 1;
11037a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginEndIndex = 0;
11047a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        } else {
11057a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginStartIndex = 0;
11067a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginEndIndex = 1;
11077a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        }
11087a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1109e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // System views measure first.
1110e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
111176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        int navWidth = 0;
1112e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mNavButtonView)) {
1113e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mNavButtonView, widthMeasureSpec, width, heightMeasureSpec, 0,
1114e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    mMaxButtonHeight);
111576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            navWidth = mNavButtonView.getMeasuredWidth() + getHorizontalMargins(mNavButtonView);
1116e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mNavButtonView.getMeasuredHeight() +
1117e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mNavButtonView));
1118e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mNavButtonView.getMeasuredState());
1119e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1120e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1121e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mCollapseButtonView)) {
1122e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mCollapseButtonView, widthMeasureSpec, width,
1123e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    heightMeasureSpec, 0, mMaxButtonHeight);
1124e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            navWidth = mCollapseButtonView.getMeasuredWidth() +
1125e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getHorizontalMargins(mCollapseButtonView);
1126e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            height = Math.max(height, mCollapseButtonView.getMeasuredHeight() +
1127e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getVerticalMargins(mCollapseButtonView));
1128e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childState = combineMeasuredStates(childState, mCollapseButtonView.getMeasuredState());
1129e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1130e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
11317a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int contentInsetStart = getContentInsetStart();
11327a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        width += Math.max(contentInsetStart, navWidth);
11337a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[marginStartIndex] = Math.max(0, contentInsetStart - navWidth);
113476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
113576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        int menuWidth = 0;
1136e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mMenuView)) {
1137e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mMenuView, widthMeasureSpec, width, heightMeasureSpec, 0,
1138e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    mMaxButtonHeight);
113976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            menuWidth = mMenuView.getMeasuredWidth() + getHorizontalMargins(mMenuView);
1140e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mMenuView.getMeasuredHeight() +
1141e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mMenuView));
1142e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mMenuView.getMeasuredState());
1143e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1144e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
11457a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int contentInsetEnd = getContentInsetEnd();
11467a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        width += Math.max(contentInsetEnd, menuWidth);
11477a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[marginEndIndex] = Math.max(0, contentInsetEnd - menuWidth);
114876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
1149e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mExpandedActionView)) {
11507a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += measureChildCollapseMargins(mExpandedActionView, widthMeasureSpec, width,
11517a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, 0, collapsingMargins);
1152e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            height = Math.max(height, mExpandedActionView.getMeasuredHeight() +
1153e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getVerticalMargins(mExpandedActionView));
1154e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childState = combineMeasuredStates(childState, mExpandedActionView.getMeasuredState());
1155e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1156e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1157e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mLogoView)) {
11587a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += measureChildCollapseMargins(mLogoView, widthMeasureSpec, width,
11597a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, 0, collapsingMargins);
1160e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mLogoView.getMeasuredHeight() +
1161e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mLogoView));
1162e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mLogoView.getMeasuredState());
1163e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1164e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1165b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        final int childCount = getChildCount();
1166b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        for (int i = 0; i < childCount; i++) {
1167b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            final View child = getChildAt(i);
1168b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1169b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            if (lp.mViewType != LayoutParams.CUSTOM || !shouldLayout(child)) {
1170b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                // We already got all system views above. Skip them and GONE views.
1171b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                continue;
1172b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            }
1173b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell
1174b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            width += measureChildCollapseMargins(child, widthMeasureSpec, width,
1175b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                    heightMeasureSpec, 0, collapsingMargins);
1176b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            height = Math.max(height, child.getMeasuredHeight() + getVerticalMargins(child));
1177b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            childState = combineMeasuredStates(childState, child.getMeasuredState());
1178b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        }
1179b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell
1180e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleWidth = 0;
1181e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleHeight = 0;
1182e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int titleVertMargins = mTitleMarginTop + mTitleMarginBottom;
1183e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int titleHorizMargins = mTitleMarginStart + mTitleMarginEnd;
1184e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mTitleTextView)) {
11857a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            titleWidth = measureChildCollapseMargins(mTitleTextView, widthMeasureSpec,
11867a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    width + titleHorizMargins, heightMeasureSpec, titleVertMargins,
11877a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    collapsingMargins);
1188e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleWidth = mTitleTextView.getMeasuredWidth() + getHorizontalMargins(mTitleTextView);
1189e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight = mTitleTextView.getMeasuredHeight() + getVerticalMargins(mTitleTextView);
1190e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mTitleTextView.getMeasuredState());
1191e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1192e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mSubtitleTextView)) {
11937a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            titleWidth = Math.max(titleWidth, measureChildCollapseMargins(mSubtitleTextView,
11947a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    widthMeasureSpec, width + titleHorizMargins,
11957a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, titleHeight + titleVertMargins,
11967a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    collapsingMargins));
1197e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight += mSubtitleTextView.getMeasuredHeight() +
1198e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mSubtitleTextView);
1199e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mSubtitleTextView.getMeasuredState());
1200e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1201e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1202e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        width += titleWidth;
1203e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        height = Math.max(height, titleHeight);
1204e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1205e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Measurement already took padding into account for available space for the children,
1206e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // add it in for the final size.
1207e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        width += getPaddingLeft() + getPaddingRight();
1208e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        height += getPaddingTop() + getPaddingBottom();
1209e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1210e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int measuredWidth = resolveSizeAndState(
1211e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                Math.max(width, getSuggestedMinimumWidth()),
1212e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                widthMeasureSpec, childState & MEASURED_STATE_MASK);
1213e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int measuredHeight = resolveSizeAndState(
1214e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                Math.max(height, getSuggestedMinimumHeight()),
1215e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT);
1216360c1d89227570510de7bb11195c986300488464Adam Powell
1217360c1d89227570510de7bb11195c986300488464Adam Powell        setMeasuredDimension(measuredWidth, shouldCollapse() ? 0 : measuredHeight);
1218e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1219e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1220e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1221e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
1222e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
1223e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int width = getWidth();
1224e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int height = getHeight();
1225e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingLeft = getPaddingLeft();
1226e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingRight = getPaddingRight();
1227e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingTop = getPaddingTop();
1228e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingBottom = getPaddingBottom();
1229e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int left = paddingLeft;
1230e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int right = width - paddingRight;
1231e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
12327a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int[] collapsingMargins = mTempMargins;
12337a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = collapsingMargins[1] = 0;
12347a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1235e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mNavButtonView)) {
1236e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
12377a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mNavButtonView, right, collapsingMargins);
1238e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
12397a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mNavButtonView, left, collapsingMargins);
1240e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1241e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1242e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1243e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mCollapseButtonView)) {
1244e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (isRtl) {
12457a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mCollapseButtonView, right, collapsingMargins);
1246e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            } else {
12477a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mCollapseButtonView, left, collapsingMargins);
1248e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1249e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1250e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1251e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mMenuView)) {
1252e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
12537a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mMenuView, left, collapsingMargins);
1254e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
12557a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mMenuView, right, collapsingMargins);
1256e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1257e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1258e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
12597a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, getContentInsetLeft() - left);
12607a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, getContentInsetRight() - (width - paddingRight - right));
126176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        left = Math.max(left, getContentInsetLeft());
126276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        right = Math.min(right, width - paddingRight - getContentInsetRight());
126376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
1264e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mExpandedActionView)) {
1265e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (isRtl) {
12667a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mExpandedActionView, right, collapsingMargins);
1267e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            } else {
12687a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mExpandedActionView, left, collapsingMargins);
1269e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1270e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1271e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1272e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mLogoView)) {
1273e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
12747a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mLogoView, right, collapsingMargins);
1275e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
12767a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mLogoView, left, collapsingMargins);
1277e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1278e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1279e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1280e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean layoutTitle = shouldLayout(mTitleTextView);
1281e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean layoutSubtitle = shouldLayout(mSubtitleTextView);
1282e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleHeight = 0;
1283e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutTitle) {
1284e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1285e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight += lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
1286e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1287e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutSubtitle) {
1288e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1289b3e411458cd7b96bb308e2de3086efc4da10d64dAdam Powell            titleHeight += lp.topMargin + mSubtitleTextView.getMeasuredHeight() + lp.bottomMargin;
1290e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1291e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1292e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutTitle || layoutSubtitle) {
1293e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            int titleTop;
1294e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View topChild = layoutTitle ? mTitleTextView : mSubtitleTextView;
1295e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View bottomChild = layoutSubtitle ? mSubtitleTextView : mTitleTextView;
1296e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams toplp = (LayoutParams) topChild.getLayoutParams();
1297e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams bottomlp = (LayoutParams) bottomChild.getLayoutParams();
1298e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1299e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) {
1300e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.TOP:
1301e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    titleTop = getPaddingTop() + toplp.topMargin + mTitleMarginTop;
1302e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1303e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                default:
1304e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.CENTER_VERTICAL:
1305e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int space = height - paddingTop - paddingBottom;
1306e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    int spaceAbove = (space - titleHeight) / 2;
1307e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    if (spaceAbove < toplp.topMargin + mTitleMarginTop) {
1308e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        spaceAbove = toplp.topMargin + mTitleMarginTop;
1309e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    } else {
1310e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        final int spaceBelow = height - paddingBottom - titleHeight -
1311e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                                spaceAbove - paddingTop;
1312e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) {
1313e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                            spaceAbove = Math.max(0, spaceAbove -
1314e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                                    (bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow));
1315e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        }
1316e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
1317e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = paddingTop + spaceAbove;
1318e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1319e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.BOTTOM:
1320e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    titleTop = height - paddingBottom - bottomlp.bottomMargin - mTitleMarginBottom -
1321e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            titleHeight;
1322e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1323e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1324e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
13257a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                final int rd = mTitleMarginStart - collapsingMargins[1];
13267a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right -= Math.max(0, rd);
13277a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                collapsingMargins[1] = Math.max(0, -rd);
1328e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int titleRight = right;
1329e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int subtitleRight = right;
13307a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1331e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutTitle) {
1332e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1333e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleLeft = titleRight - mTitleTextView.getMeasuredWidth();
1334e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
1335e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
13367a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    titleRight = titleLeft - mTitleMarginEnd;
1337e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = titleBottom + lp.bottomMargin;
1338e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1339e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutSubtitle) {
1340e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1341e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop += lp.topMargin;
1342e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleLeft = subtitleRight - mSubtitleTextView.getMeasuredWidth();
1343e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
1344e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
13457a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    subtitleRight = subtitleRight - mTitleMarginEnd;
1346e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = subtitleBottom + lp.bottomMargin;
1347e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1348c80f95228560e5912e97886ed637f4d538e87c70Adam Powell                right = Math.min(titleRight, subtitleRight);
1349e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
13507a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                final int ld = mTitleMarginStart - collapsingMargins[0];
13517a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left += Math.max(0, ld);
13527a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                collapsingMargins[0] = Math.max(0, -ld);
1353e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int titleLeft = left;
1354e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int subtitleLeft = left;
13557a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1356e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutTitle) {
1357e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1358e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleRight = titleLeft + mTitleTextView.getMeasuredWidth();
1359e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
1360e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
13617a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    titleLeft = titleRight + mTitleMarginEnd;
1362e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = titleBottom + lp.bottomMargin;
1363e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1364e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutSubtitle) {
1365e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1366e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop += lp.topMargin;
1367e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleRight = subtitleLeft + mSubtitleTextView.getMeasuredWidth();
1368e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
1369e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
13707a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    subtitleLeft = subtitleRight + mTitleMarginEnd;
1371e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = subtitleBottom + lp.bottomMargin;
1372e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1373e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                left = Math.max(titleLeft, subtitleLeft);
1374e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1375e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1376e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1377e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Get all remaining children sorted for layout. This is all prepared
1378e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // such that absolute layout direction can be used below.
1379e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1380e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.LEFT);
1381e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int leftViewsCount = mTempViews.size();
1382e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < leftViewsCount; i++) {
13837a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            left = layoutChildLeft(mTempViews.get(i), left, collapsingMargins);
1384e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1385e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1386e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.RIGHT);
1387e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int rightViewsCount = mTempViews.size();
1388e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < rightViewsCount; i++) {
13897a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            right = layoutChildRight(mTempViews.get(i), right, collapsingMargins);
1390e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1391e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1392e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Centered views try to center with respect to the whole bar, but views pinned
1393e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // to the left or right can push the mass of centered views to one side or the other.
1394e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.CENTER_HORIZONTAL);
13957a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int centerViewsWidth = getViewListMeasuredWidth(mTempViews, collapsingMargins);
1396e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2;
1397e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int halfCenterViewsWidth = centerViewsWidth / 2;
1398e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int centerLeft = parentCenter - halfCenterViewsWidth;
1399e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int centerRight = centerLeft + centerViewsWidth;
1400e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (centerLeft < left) {
1401e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            centerLeft = left;
1402e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (centerRight > right) {
1403e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            centerLeft -= centerRight - right;
1404e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1405e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1406e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int centerViewsCount = mTempViews.size();
1407e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < centerViewsCount; i++) {
14087a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            centerLeft = layoutChildLeft(mTempViews.get(i), centerLeft, collapsingMargins);
1409e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1410e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTempViews.clear();
1411e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1412e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
14137a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int getViewListMeasuredWidth(List<View> views, int[] collapsingMargins) {
14147a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        int collapseLeft = collapsingMargins[0];
14157a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        int collapseRight = collapsingMargins[1];
1416e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int width = 0;
1417e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int count = views.size();
1418e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < count; i++) {
1419e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final View v = views.get(i);
1420e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) v.getLayoutParams();
14217a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int l = lp.leftMargin - collapseLeft;
14227a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int r = lp.rightMargin - collapseRight;
14237a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int leftMargin = Math.max(0, l);
14247a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int rightMargin = Math.max(0, r);
14257a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            collapseLeft = Math.max(0, -l);
14267a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            collapseRight = Math.max(0, -r);
14277a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += leftMargin + v.getMeasuredWidth() + rightMargin;
1428e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1429e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return width;
1430e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1431e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
14327a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int layoutChildLeft(View child, int left, int[] collapsingMargins) {
1433e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
14347a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int l = lp.leftMargin - collapsingMargins[0];
14357a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        left += Math.max(0, l);
14367a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, -l);
14377a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int top = getChildTop(child);
14387a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int childWidth = child.getMeasuredWidth();
14397a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        child.layout(left, top, left + childWidth, top + child.getMeasuredHeight());
14407a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        left += childWidth + lp.rightMargin;
1441e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return left;
1442e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1443e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
14447a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int layoutChildRight(View child, int right, int[] collapsingMargins) {
1445e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
14467a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int r = lp.rightMargin - collapsingMargins[1];
14477a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        right -= Math.max(0, r);
14487a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, -r);
14497a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int top = getChildTop(child);
14507a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int childWidth = child.getMeasuredWidth();
14517a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        child.layout(right - childWidth, top, right, top + child.getMeasuredHeight());
14527a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        right -= childWidth + lp.leftMargin;
1453e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return right;
1454e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1455e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1456e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildTop(View child) {
1457e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1458e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (getChildVerticalGravity(lp.gravity)) {
1459e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.TOP:
1460e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return getPaddingTop();
1461e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1462e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.BOTTOM:
1463e002c2fb39f9df1db38a7412832882ae622660d4Adam Powell                return getHeight() - getPaddingBottom() -
1464e002c2fb39f9df1db38a7412832882ae622660d4Adam Powell                        child.getMeasuredHeight() - lp.bottomMargin;
1465e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1466e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1467e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_VERTICAL:
1468e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int paddingTop = getPaddingTop();
1469e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int paddingBottom = getPaddingBottom();
1470e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int height = getHeight();
1471e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int childHeight = child.getMeasuredHeight();
1472e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int space = height - paddingTop - paddingBottom;
1473e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int spaceAbove = (space - childHeight) / 2;
1474e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (spaceAbove < lp.topMargin) {
1475e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    spaceAbove = lp.topMargin;
1476e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                } else {
1477e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int spaceBelow = height - paddingBottom - childHeight -
1478e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                            spaceAbove - paddingTop;
1479e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    if (spaceBelow < lp.bottomMargin) {
1480e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        spaceAbove = Math.max(0, spaceAbove - (lp.bottomMargin - spaceBelow));
1481e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
1482e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1483e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return paddingTop + spaceAbove;
1484e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1485e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1486e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1487e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildVerticalGravity(int gravity) {
1488e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int vgrav = gravity & Gravity.VERTICAL_GRAVITY_MASK;
1489e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (vgrav) {
1490e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.TOP:
1491e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.BOTTOM:
1492e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_VERTICAL:
1493e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return vgrav;
1494e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1495e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return mGravity & Gravity.VERTICAL_GRAVITY_MASK;
1496e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1497e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1498e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1499e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1500e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Prepare a list of non-SYSTEM child views. If the layout direction is RTL
1501e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * this will be in reverse child order.
1502e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1503e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param views List to populate. It will be cleared before use.
1504e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param gravity Horizontal gravity to match against
1505e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1506e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void addCustomViewsWithGravity(List<View> views, int gravity) {
1507e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
1508e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int childCount = getChildCount();
1509e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int absGrav = Gravity.getAbsoluteGravity(gravity, getLayoutDirection());
1510e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1511e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        views.clear();
1512e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1513e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (isRtl) {
1514e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            for (int i = childCount - 1; i >= 0; i--) {
1515e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final View child = getChildAt(i);
1516e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1517e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
1518e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        getChildHorizontalGravity(lp.gravity) == absGrav) {
1519e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    views.add(child);
1520e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1521e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1522e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else {
1523e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            for (int i = 0; i < childCount; i++) {
1524e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final View child = getChildAt(i);
1525e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1526e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
1527e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        getChildHorizontalGravity(lp.gravity) == absGrav) {
1528e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    views.add(child);
1529e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1530e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1531e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1532e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1533e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1534e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildHorizontalGravity(int gravity) {
1535e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int ld = getLayoutDirection();
1536e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int absGrav = Gravity.getAbsoluteGravity(gravity, ld);
1537e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int hGrav = absGrav & Gravity.HORIZONTAL_GRAVITY_MASK;
1538e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (hGrav) {
1539e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.LEFT:
1540e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.RIGHT:
1541e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_HORIZONTAL:
1542e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return hGrav;
1543e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1544e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return ld == LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT;
1545e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1546e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1547e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1548e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private boolean shouldLayout(View view) {
1549e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return view != null && view.getParent() == this && view.getVisibility() != GONE;
1550e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1551e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1552e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getHorizontalMargins(View v) {
1553e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
1554e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mlp.getMarginStart() + mlp.getMarginEnd();
1555e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1556e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1557e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getVerticalMargins(View v) {
1558e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
1559e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mlp.topMargin + mlp.bottomMargin;
1560e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1561e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1562e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1563e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public LayoutParams generateLayoutParams(AttributeSet attrs) {
1564e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return new LayoutParams(getContext(), attrs);
1565e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1566e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1567e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1568e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1569e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (p instanceof LayoutParams) {
1570e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams((LayoutParams) p);
1571e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        } else if (p instanceof ActionBar.LayoutParams) {
1572e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return new LayoutParams((ActionBar.LayoutParams) p);
1573e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (p instanceof MarginLayoutParams) {
1574e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams((MarginLayoutParams) p);
1575e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else {
1576e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams(p);
1577e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1578e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1579e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1580e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1581e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    protected LayoutParams generateDefaultLayoutParams() {
1582e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
1583e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1584e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1585e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1586e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1587e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return super.checkLayoutParams(p) && p instanceof LayoutParams;
1588e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1589e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1590e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private static boolean isCustomView(View child) {
1591e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return ((LayoutParams) child.getLayoutParams()).mViewType == LayoutParams.CUSTOM;
1592e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1593e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1594e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
1595e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public DecorToolbar getWrapper() {
1596e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mWrapper == null) {
15972aa09a94dd540650d3ebad7363e4fb3aab2ebc95Adam Powell            mWrapper = new ToolbarWidgetWrapper(this, true);
1598e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1599e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mWrapper;
1600e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1601e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1602e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void setChildVisibilityForExpandedActionView(boolean expand) {
1603e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int childCount = getChildCount();
1604e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        for (int i = 0; i < childCount; i++) {
1605e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View child = getChildAt(i);
1606e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1607e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (lp.mViewType != LayoutParams.EXPANDED && child != mMenuView) {
1608e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                child.setVisibility(expand ? GONE : VISIBLE);
1609e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1610e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1611e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1612e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1613e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1614360c1d89227570510de7bb11195c986300488464Adam Powell     * Force the toolbar to collapse to zero-height during measurement if
1615360c1d89227570510de7bb11195c986300488464Adam Powell     * it could be considered "empty" (no visible elements with nonzero measured size)
1616360c1d89227570510de7bb11195c986300488464Adam Powell     * @hide
1617360c1d89227570510de7bb11195c986300488464Adam Powell     */
1618360c1d89227570510de7bb11195c986300488464Adam Powell    public void setCollapsible(boolean collapsible) {
1619360c1d89227570510de7bb11195c986300488464Adam Powell        mCollapsible = collapsible;
1620360c1d89227570510de7bb11195c986300488464Adam Powell        requestLayout();
1621360c1d89227570510de7bb11195c986300488464Adam Powell    }
1622360c1d89227570510de7bb11195c986300488464Adam Powell
1623360c1d89227570510de7bb11195c986300488464Adam Powell    /**
1624e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Interface responsible for receiving menu item click events if the items themselves
1625e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * do not have individual item click listeners.
1626e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1627e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public interface OnMenuItemClickListener {
1628e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        /**
1629e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * This method will be invoked when a menu item is clicked if the item itself did
1630e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * not already handle the event.
1631e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         *
1632e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * @param item {@link MenuItem} that was clicked
1633e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * @return <code>true</code> if the event was handled, <code>false</code> otherwise.
1634e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         */
1635e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public boolean onMenuItemClick(MenuItem item);
1636e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1637e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1638e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1639e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Layout information for child views of Toolbars.
1640e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1641d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * <p>Toolbar.LayoutParams extends ActionBar.LayoutParams for compatibility with existing
1642d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * ActionBar API. See {@link android.app.Activity#setActionBar(Toolbar) Activity.setActionBar}
1643d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * for more info on how to use a Toolbar as your Activity's ActionBar.</p>
1644d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     *
1645e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @attr ref android.R.styleable#Toolbar_LayoutParams_layout_gravity
1646e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1647e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public static class LayoutParams extends ActionBar.LayoutParams {
1648e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        static final int CUSTOM = 0;
1649e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        static final int SYSTEM = 1;
1650e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        static final int EXPANDED = 2;
1651e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1652e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int mViewType = CUSTOM;
1653e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1654e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(@NonNull Context c, AttributeSet attrs) {
1655e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(c, attrs);
1656e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1657e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1658e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int width, int height) {
1659e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(width, height);
1660e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this.gravity = Gravity.CENTER_VERTICAL | Gravity.START;
1661e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1662e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1663e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int width, int height, int gravity) {
1664e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(width, height);
1665e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this.gravity = gravity;
1666e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1667e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1668e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int gravity) {
1669e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this(WRAP_CONTENT, MATCH_PARENT, gravity);
1670e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1671e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1672e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(LayoutParams source) {
1673e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1674e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1675e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mViewType = source.mViewType;
1676e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1677e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1678e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public LayoutParams(ActionBar.LayoutParams source) {
1679e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            super(source);
1680e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1681e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1682e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(MarginLayoutParams source) {
1683e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1684d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            // ActionBar.LayoutParams doesn't have a MarginLayoutParams constructor.
1685d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            // Fake it here and copy over the relevant data.
1686d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            copyMarginsFrom(source);
1687e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1688e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1689e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(ViewGroup.LayoutParams source) {
1690e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1691e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1692e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1693e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1694e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    static class SavedState extends BaseSavedState {
1695e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public SavedState(Parcel source) {
1696e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1697e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1698e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1699e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public SavedState(Parcelable superState) {
1700e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(superState);
1701e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1702e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1703e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        @Override
1704e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public void writeToParcel(Parcel out, int flags) {
1705e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super.writeToParcel(out, flags);
1706e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1707e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1708e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
1709e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1710e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            @Override
1711e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            public SavedState createFromParcel(Parcel source) {
1712e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return new SavedState(source);
1713e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1714e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1715e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            @Override
1716e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            public SavedState[] newArray(int size) {
1717e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return new SavedState[size];
1718e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1719e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        };
1720e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1721e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1722e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private class ExpandedActionViewMenuPresenter implements MenuPresenter {
1723e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        MenuBuilder mMenu;
1724e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        MenuItemImpl mCurrentExpandedItem;
1725e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1726e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1727e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void initForMenu(Context context, MenuBuilder menu) {
1728e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Clear the expanded action view when menus change.
1729e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mMenu != null && mCurrentExpandedItem != null) {
1730e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mMenu.collapseItemActionView(mCurrentExpandedItem);
1731e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1732e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenu = menu;
1733e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1734e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1735e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1736e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public MenuView getMenuView(ViewGroup root) {
1737e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return null;
1738e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1739e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1740e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1741e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void updateMenuView(boolean cleared) {
1742e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Make sure the expanded item we have is still there.
1743e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mCurrentExpandedItem != null) {
1744e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                boolean found = false;
1745e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1746e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (mMenu != null) {
1747e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    final int count = mMenu.size();
1748e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    for (int i = 0; i < count; i++) {
1749e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        final MenuItem item = mMenu.getItem(i);
1750e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        if (item == mCurrentExpandedItem) {
1751e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            found = true;
1752e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            break;
1753e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        }
1754e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    }
1755e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
1756e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1757e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (!found) {
1758e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    // The item we had expanded disappeared. Collapse.
1759e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    collapseItemActionView(mMenu, mCurrentExpandedItem);
1760e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
1761e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1762e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1763e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1764e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1765e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void setCallback(Callback cb) {
1766e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1767e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1768e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1769e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
1770e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
1771e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1772e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1773e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1774e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
1775e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1776e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1777e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1778e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean flagActionItems() {
1779e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
1780e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1781e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1782e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1783e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
1784e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureCollapseButtonView();
1785e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mCollapseButtonView.getParent() != Toolbar.this) {
1786e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                addView(mCollapseButtonView);
1787e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1788e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedActionView = item.getActionView();
1789e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCurrentExpandedItem = item;
1790e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView.getParent() != Toolbar.this) {
1791e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                final LayoutParams lp = generateDefaultLayoutParams();
1792e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
1793e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                lp.mViewType = LayoutParams.EXPANDED;
1794e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedActionView.setLayoutParams(lp);
1795e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                addView(mExpandedActionView);
1796e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1797e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1798e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            setChildVisibilityForExpandedActionView(true);
1799e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            requestLayout();
1800e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.setActionViewExpanded(true);
1801e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1802e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView instanceof CollapsibleActionView) {
1803e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                ((CollapsibleActionView) mExpandedActionView).onActionViewExpanded();
1804e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1805e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1806e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return true;
1807e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1808e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1809e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1810e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
1811e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Do this before detaching the actionview from the hierarchy, in case
1812e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // it needs to dismiss the soft keyboard, etc.
1813e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView instanceof CollapsibleActionView) {
1814e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                ((CollapsibleActionView) mExpandedActionView).onActionViewCollapsed();
1815e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1816e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1817e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            removeView(mExpandedActionView);
1818e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            removeView(mCollapseButtonView);
1819e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedActionView = null;
1820e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1821e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            setChildVisibilityForExpandedActionView(false);
1822e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCurrentExpandedItem = null;
1823e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            requestLayout();
1824e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.setActionViewExpanded(false);
1825e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1826e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return true;
1827e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1828e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1829e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1830e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public int getId() {
1831e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return 0;
1832e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1833e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1834e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1835e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public Parcelable onSaveInstanceState() {
1836e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return null;
1837e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1838e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1839e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1840e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void onRestoreInstanceState(Parcelable state) {
1841e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1842e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1843e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell}
1844