Toolbar.java revision b606a3a312fe6d69d97c66d76803eb2412d8329a
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        }
236e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        a.recycle();
2373d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
2383d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        mPopupContext = context;
2393d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        mPopupTheme = 0;
2403d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    }
2413d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
2423d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /**
2433d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * Specifies the theme to use when inflating popup menus. By default, uses
2443d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * the same theme as the toolbar itself.
2453d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     *
2463d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @param resId theme used to inflate popup menus
2473d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @see #getPopupTheme()
2483d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     */
2493d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    public void setPopupTheme(int resId) {
2503d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        if (mPopupTheme != resId) {
2513d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mPopupTheme = resId;
2523d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            if (resId == 0) {
2533d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette                mPopupContext = mContext;
2543d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            } else {
2553d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette                mPopupContext = new ContextThemeWrapper(mContext, resId);
2563d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            }
2573d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        }
2583d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    }
2593d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
2603d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /**
2613d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @return resource identifier of the theme used to inflate popup menus, or
2623d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     *         0 if menus are inflated against the toolbar theme
2633d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @see #setPopupTheme(int)
2643d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     */
2653d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    public int getPopupTheme() {
2663d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        return mPopupTheme;
267e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
268e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
26976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    @Override
27076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) {
27176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        super.onRtlPropertiesChanged(layoutDirection);
27276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setDirection(layoutDirection == LAYOUT_DIRECTION_RTL);
27376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
27476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
275e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
276e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a logo drawable from a resource id.
277e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
278e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This drawable should generally take the place of title text. The logo cannot be
279e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * clicked. Apps using a logo should also supply a description using
280e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setLogoDescription(int)}.</p>
281e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
282e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId ID of a drawable resource
283e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
284e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogo(int resId) {
285e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setLogo(getContext().getDrawable(resId));
286e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
287e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
288e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
289e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean canShowOverflowMenu() {
290e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return getVisibility() == VISIBLE && mMenuView != null && mMenuView.isOverflowReserved();
291e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
292e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
293e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
294e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Check whether the overflow menu is currently showing. This may not reflect
295e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * a pending show operation in progress.
296e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
297e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the overflow menu is currently showing
298e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
299e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isOverflowMenuShowing() {
300e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.isOverflowMenuShowing();
301e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
302e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
303e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
304e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isOverflowMenuShowPending() {
305e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.isOverflowMenuShowPending();
306e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
307e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
308e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
309e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Show the overflow items from the associated menu.
310e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
311e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the menu was able to be shown, false otherwise
312e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
313e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean showOverflowMenu() {
314e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.showOverflowMenu();
315e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
316e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
317e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
318e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Hide the overflow items from the associated menu.
319e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
320e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the menu was able to be hidden, false otherwise
321e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
322e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean hideOverflowMenu() {
323e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.hideOverflowMenu();
324e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
325e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
326e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
327e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void setMenu(MenuBuilder menu, ActionMenuPresenter outerPresenter) {
328e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (menu == null && mMenuView == null) {
329e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return;
330e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
331e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
332e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        ensureMenuView();
333e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MenuBuilder oldMenu = mMenuView.peekMenu();
334e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (oldMenu == menu) {
335e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return;
336e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
337e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
338e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (oldMenu != null) {
339e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            oldMenu.removeMenuPresenter(mOuterActionMenuPresenter);
340e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            oldMenu.removeMenuPresenter(mExpandedMenuPresenter);
341e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
342e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
343e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mExpandedMenuPresenter == null) {
344e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
345e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
346e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
347e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        outerPresenter.setExpandedActionViewsExclusive(true);
348e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (menu != null) {
3493d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(outerPresenter, mPopupContext);
3503d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(mExpandedMenuPresenter, mPopupContext);
351e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        } else {
3523d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            outerPresenter.initForMenu(mPopupContext, null);
3533d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mExpandedMenuPresenter.initForMenu(mPopupContext, null);
354e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            outerPresenter.updateMenuView(true);
355e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedMenuPresenter.updateMenuView(true);
356e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
3573d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        mMenuView.setPopupTheme(mPopupTheme);
358e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mMenuView.setPresenter(outerPresenter);
359e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mOuterActionMenuPresenter = outerPresenter;
360e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
361e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
362e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
363e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Dismiss all currently showing popup menus, including overflow or submenus.
364e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
365e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void dismissPopupMenus() {
366e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mMenuView != null) {
367e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenuView.dismissPopupMenus();
368e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
369e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
370e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
371e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
372e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isTitleTruncated() {
373e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mTitleTextView == null) {
374e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
375e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
376e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
377e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final Layout titleLayout = mTitleTextView.getLayout();
378e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (titleLayout == null) {
379e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
380e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
381e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
382e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int lineCount = titleLayout.getLineCount();
383e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        for (int i = 0; i < lineCount; i++) {
384e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (titleLayout.getEllipsisCount(i) > 0) {
385e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                return true;
386e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
387e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
388e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return false;
389e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
390e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
391e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
392e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a logo drawable.
393e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
394e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This drawable should generally take the place of title text. The logo cannot be
395e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * clicked. Apps using a logo should also supply a description using
396e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setLogoDescription(int)}.</p>
397e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
398e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param drawable Drawable to use as a logo
399e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
400e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogo(Drawable drawable) {
401e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (drawable != null) {
402e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureLogoView();
403e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mLogoView.getParent() == null) {
404e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mLogoView);
405e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
406e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mLogoView != null && mLogoView.getParent() != null) {
407e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mLogoView);
408e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
409e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mLogoView != null) {
410e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mLogoView.setImageDrawable(drawable);
411e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
412e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
413e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
414e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
415e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the current logo drawable.
416e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
417e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The current logo drawable
418e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setLogo(int)
419e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setLogo(android.graphics.drawable.Drawable)
420e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
421e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Drawable getLogo() {
422e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mLogoView != null ? mLogoView.getDrawable() : null;
423e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
424e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
425e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
426e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description of the toolbar's logo.
427e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
428e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description will be used for accessibility or other similar descriptions
429e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * of the UI.</p>
430e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
431e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId String resource id
432e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
433e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogoDescription(int resId) {
434e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setLogoDescription(getContext().getText(resId));
435e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
436e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
437e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
438e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description of the toolbar's logo.
439e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
440e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description will be used for accessibility or other similar descriptions
441e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * of the UI.</p>
442e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
443e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param description Description to set
444e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
445e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogoDescription(CharSequence description) {
446e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (!TextUtils.isEmpty(description)) {
447e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureLogoView();
448e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
449e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mLogoView != null) {
450e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mLogoView.setContentDescription(description);
451e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
452e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
453e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
454e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
455e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the description of the toolbar's logo.
456e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
457e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return A description of the logo
458e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
459e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getLogoDescription() {
460e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mLogoView != null ? mLogoView.getContentDescription() : null;
461e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
462e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
463e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureLogoView() {
464e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mLogoView == null) {
465e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mLogoView = new ImageView(getContext());
466e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
467e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
468e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
469e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
470e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Check whether this Toolbar is currently hosting an expanded action view.
471e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
472e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * <p>An action view may be expanded either directly from the
473e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * {@link android.view.MenuItem MenuItem} it belongs to or by user action. If the Toolbar
474e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * has an expanded action view it can be collapsed using the {@link #collapseActionView()}
475e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * method.</p>
476e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
477e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the Toolbar has an expanded action view
478e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
479e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean hasExpandedActionView() {
480e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mExpandedMenuPresenter != null &&
481e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedMenuPresenter.mCurrentExpandedItem != null;
482e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
483e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
484e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
485e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Collapse a currently expanded action view. If this Toolbar does not have an
486e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * expanded action view this method has no effect.
487e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
488e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * <p>An action view may be expanded either directly from the
489e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * {@link android.view.MenuItem MenuItem} it belongs to or by user action.</p>
490e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
491e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @see #hasExpandedActionView()
492e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
493e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void collapseActionView() {
494e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MenuItemImpl item = mExpandedMenuPresenter == null ? null :
495e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedMenuPresenter.mCurrentExpandedItem;
496e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (item != null) {
497e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.collapseActionView();
498e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
499e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
500e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
501e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
502e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Returns the title of this toolbar.
503e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
504e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return The current title.
505e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
506e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getTitle() {
507e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mTitleText;
508e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
509e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
510e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
511e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the title of this toolbar.
512e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
513e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>A title should be used as the anchor for a section of content. It should
514e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * describe or name the content being viewed.</p>
515e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
516e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId Resource ID of a string to set as the title
517e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
518e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setTitle(int resId) {
519e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setTitle(getContext().getText(resId));
520e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
521e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
522e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
523e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the title of this toolbar.
524e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
525e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>A title should be used as the anchor for a section of content. It should
526e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * describe or name the content being viewed.</p>
527e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
528e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param title Title to set
529e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
530e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setTitle(CharSequence title) {
531e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(title)) {
532e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mTitleTextView == null) {
533e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final Context context = getContext();
534e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                mTitleTextView = new TextView(context);
535e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mTitleTextView.setSingleLine();
536e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
5371a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mTitleTextAppearance != 0) {
5381a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mTitleTextView.setTextAppearance(context, mTitleTextAppearance);
5391a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
5401a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mTitleTextColor != 0) {
5411a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mTitleTextView.setTextColor(mTitleTextColor);
5421a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
543e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
544e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mTitleTextView.getParent() == null) {
545e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mTitleTextView);
546e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
547e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mTitleTextView != null && mTitleTextView.getParent() != null) {
548e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mTitleTextView);
549e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
550e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mTitleTextView != null) {
551e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleTextView.setText(title);
552e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
553e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTitleText = title;
554e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
555e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
556e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
557e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the subtitle of this toolbar.
558e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
559e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The current subtitle
560e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
561e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getSubtitle() {
562e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mSubtitleText;
563e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
564e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
565e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
566e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the subtitle of this toolbar.
567e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
568e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Subtitles should express extended information about the current content.</p>
569e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
570e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId String resource ID
571e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
572e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setSubtitle(int resId) {
573e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setSubtitle(getContext().getText(resId));
574e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
575e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
576e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
577e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the subtitle of this toolbar.
578e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
579e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Subtitles should express extended information about the current content.</p>
580e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
581e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param subtitle Subtitle to set
582e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
583e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setSubtitle(CharSequence subtitle) {
584e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(subtitle)) {
585e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mSubtitleTextView == null) {
586e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final Context context = getContext();
587e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                mSubtitleTextView = new TextView(context);
588e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mSubtitleTextView.setSingleLine();
589e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mSubtitleTextView.setEllipsize(TextUtils.TruncateAt.END);
5901a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mSubtitleTextAppearance != 0) {
5911a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mSubtitleTextView.setTextAppearance(context, mSubtitleTextAppearance);
5921a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
5931a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mSubtitleTextColor != 0) {
5941a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mSubtitleTextView.setTextColor(mSubtitleTextColor);
5951a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
596e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
597e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mSubtitleTextView.getParent() == null) {
598e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mSubtitleTextView);
599e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
600e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mSubtitleTextView != null && mSubtitleTextView.getParent() != null) {
601e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mSubtitleTextView);
602e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
603e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mSubtitleTextView != null) {
604e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mSubtitleTextView.setText(subtitle);
605e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
606e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mSubtitleText = subtitle;
607e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
608e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
609e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
610ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * Sets the text color, size, style, hint color, and highlight color
611ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * from the specified TextAppearance resource.
612ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     */
613ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    public void setTitleTextAppearance(Context context, int resId) {
614ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        mTitleTextAppearance = resId;
615ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        if (mTitleTextView != null) {
616ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell            mTitleTextView.setTextAppearance(context, resId);
617ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        }
618ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    }
619ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell
620ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    /**
621ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * Sets the text color, size, style, hint color, and highlight color
622ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * from the specified TextAppearance resource.
623ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     */
624ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    public void setSubtitleTextAppearance(Context context, int resId) {
625ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        mSubtitleTextAppearance = resId;
626ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        if (mSubtitleTextView != null) {
627ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell            mSubtitleTextView.setTextAppearance(context, resId);
628ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        }
629ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    }
630ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell
631ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    /**
6321a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * Sets the text color of the title, if present.
6331a264ce3d915b187b71101436bec3328c76288b0Adam Powell     *
6341a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * @param color The new text color in 0xAARRGGBB format
6351a264ce3d915b187b71101436bec3328c76288b0Adam Powell     */
6361a264ce3d915b187b71101436bec3328c76288b0Adam Powell    public void setTitleTextColor(int color) {
6371a264ce3d915b187b71101436bec3328c76288b0Adam Powell        mTitleTextColor = color;
6381a264ce3d915b187b71101436bec3328c76288b0Adam Powell        if (mTitleTextView != null) {
6391a264ce3d915b187b71101436bec3328c76288b0Adam Powell            mTitleTextView.setTextColor(color);
6401a264ce3d915b187b71101436bec3328c76288b0Adam Powell        }
6411a264ce3d915b187b71101436bec3328c76288b0Adam Powell    }
6421a264ce3d915b187b71101436bec3328c76288b0Adam Powell
6431a264ce3d915b187b71101436bec3328c76288b0Adam Powell    /**
6441a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * Sets the text color of the subtitle, if present.
6451a264ce3d915b187b71101436bec3328c76288b0Adam Powell     *
6461a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * @param color The new text color in 0xAARRGGBB format
6471a264ce3d915b187b71101436bec3328c76288b0Adam Powell     */
6481a264ce3d915b187b71101436bec3328c76288b0Adam Powell    public void setSubtitleTextColor(int color) {
6491a264ce3d915b187b71101436bec3328c76288b0Adam Powell        mSubtitleTextColor = color;
6501a264ce3d915b187b71101436bec3328c76288b0Adam Powell        if (mSubtitleTextView != null) {
6511a264ce3d915b187b71101436bec3328c76288b0Adam Powell            mSubtitleTextView.setTextColor(color);
6521a264ce3d915b187b71101436bec3328c76288b0Adam Powell        }
6531a264ce3d915b187b71101436bec3328c76288b0Adam Powell    }
6541a264ce3d915b187b71101436bec3328c76288b0Adam Powell
6551a264ce3d915b187b71101436bec3328c76288b0Adam Powell    /**
656e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the icon to use for the toolbar's navigation button.
657e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
658e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>The navigation button appears at the start of the toolbar if present. Setting an icon
659e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * will make the navigation button visible.</p>
660e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
661e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>If you use a navigation icon you should also set a description for its action using
662e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setNavigationDescription(int)}. This is used for accessibility and tooltips.</p>
663e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
664e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId Resource ID of a drawable to set
665e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
666e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationIcon(int resId) {
667e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setNavigationIcon(getContext().getDrawable(resId));
668e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
669e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
670e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
6717901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * Retrieve the currently configured content description for the navigation button view.
6727901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * This will be used to describe the navigation action to users through mechanisms such
6737901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * as screen readers or tooltips.
6747901b2a54981b769bf27852c41ce612871a8e019Adam Powell     *
6757901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * @return The navigation button's content description
6767901b2a54981b769bf27852c41ce612871a8e019Adam Powell     */
6777901b2a54981b769bf27852c41ce612871a8e019Adam Powell    public CharSequence getNavigationContentDescription() {
6787901b2a54981b769bf27852c41ce612871a8e019Adam Powell        return mNavButtonView != null ? mNavButtonView.getContentDescription() : null;
6797901b2a54981b769bf27852c41ce612871a8e019Adam Powell    }
6807901b2a54981b769bf27852c41ce612871a8e019Adam Powell
6817901b2a54981b769bf27852c41ce612871a8e019Adam Powell    /**
682e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Set a content description for the navigation button if one is present. The content
683e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * description will be read via screen readers or other accessibility systems to explain
684e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * the action of the navigation button.
685e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
686e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @param description Content description to set
687e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
688e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void setNavigationContentDescription(CharSequence description) {
689e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        ensureNavButtonView();
690e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mNavButtonView.setContentDescription(description);
691e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
692e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
693e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
694e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Set a content description for the navigation button if one is present. The content
695e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * description will be read via screen readers or other accessibility systems to explain
696e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * the action of the navigation button.
697e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
698e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @param resId Resource ID of a content description string to set
699e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
700e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void setNavigationContentDescription(int resId) {
701e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        ensureNavButtonView();
7028e5372fe93ee2ee3bf20baab9054b39b4c5043ceAdam Powell        mNavButtonView.setContentDescription(resId != 0 ? getContext().getText(resId) : null);
703e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
704e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
705e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
706e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the icon to use for the toolbar's navigation button.
707e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
708e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>The navigation button appears at the start of the toolbar if present. Setting an icon
709e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * will make the navigation button visible.</p>
710e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
711e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>If you use a navigation icon you should also set a description for its action using
712e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setNavigationDescription(int)}. This is used for accessibility and tooltips.</p>
713e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
714e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param icon Drawable to set
715e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
716e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationIcon(Drawable icon) {
717e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (icon != null) {
718e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            ensureNavButtonView();
719e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mNavButtonView.getParent() == null) {
720e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                addSystemView(mNavButtonView);
721e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
722e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (mNavButtonView != null && mNavButtonView.getParent() != null) {
723e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mNavButtonView);
724e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
725e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView != null) {
726e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mNavButtonView.setImageDrawable(icon);
727e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
728e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
729e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
730e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
731e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the current drawable used as the navigation icon.
732e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
733e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The navigation icon drawable
734e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
735e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Drawable getNavigationIcon() {
736e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mNavButtonView != null ? mNavButtonView.getDrawable() : null;
737e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
738e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
739e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
740e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description for the navigation button.
741e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
742e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description string is used for accessibility, tooltips and other facilities
743e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * to improve discoverability.</p>
744e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
745e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId Resource ID of a string to set
746e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
747e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationDescription(int resId) {
748e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setNavigationDescription(getContext().getText(resId));
749e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
750e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
751e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
752e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description for the navigation button.
753e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
754e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description string is used for accessibility, tooltips and other facilities
755e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * to improve discoverability.</p>
756e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
757e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param description String to set as the description
758e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
759e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationDescription(CharSequence description) {
760e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(description)) {
761e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            ensureNavButtonView();
762e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
763e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView != null) {
764e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mNavButtonView.setContentDescription(description);
765e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
766e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
767e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
768e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
769e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a listener to respond to navigation events.
770e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
771e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This listener will be called whenever the user clicks the navigation button
772e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * at the start of the toolbar. An icon must be set for the navigation button to appear.</p>
773e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
774e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param listener Listener to set
775e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setNavigationIcon(android.graphics.drawable.Drawable)
776e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
777e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationOnClickListener(OnClickListener listener) {
778e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        ensureNavButtonView();
779e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mNavButtonView.setOnClickListener(listener);
780e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
781e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
782e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
783e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the Menu shown in the toolbar.
784e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
785e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Applications that wish to populate the toolbar's menu can do so from here. To use
786e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * an XML menu resource, use {@link #inflateMenu(int)}.</p>
787e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
788e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The toolbar's Menu
789e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
790e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Menu getMenu() {
79107a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        ensureMenu();
792e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView.getMenu();
793e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
794e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
79507a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell    private void ensureMenu() {
79607a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        ensureMenuView();
79707a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        if (mMenuView.peekMenu() == null) {
79807a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            // Initialize a new menu for the first time.
79907a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            final MenuBuilder menu = (MenuBuilder) mMenuView.getMenu();
80007a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            if (mExpandedMenuPresenter == null) {
80107a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell                mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
80207a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            }
80307a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            mMenuView.setExpandedActionViewsExclusive(true);
8043d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(mExpandedMenuPresenter, mPopupContext);
80507a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        }
80607a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell    }
80707a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell
808e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureMenuView() {
809e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mMenuView == null) {
810e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mMenuView = new ActionMenuView(getContext());
8113d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mMenuView.setPopupTheme(mPopupTheme);
812e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mMenuView.setOnMenuItemClickListener(mMenuViewItemClickListener);
813e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
814e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.END | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
815e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenuView.setLayoutParams(lp);
816e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            addSystemView(mMenuView);
817e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
818e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
819e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
820e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private MenuInflater getMenuInflater() {
821e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return new MenuInflater(getContext());
822e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
823e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
824e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
825e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Inflate a menu resource into this toolbar.
826e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
827e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Inflate an XML menu resource into this toolbar. Existing items in the menu will not
828e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * be modified or removed.</p>
829e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
830e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId ID of a menu resource to inflate
831e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
832e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void inflateMenu(int resId) {
833e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        getMenuInflater().inflate(resId, getMenu());
834e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
835e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
836e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
837e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a listener to respond to menu item click events.
838e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
839e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This listener will be invoked whenever a user selects a menu item from
840e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * the action buttons presented at the end of the toolbar or the associated overflow.</p>
841e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
842e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param listener Listener to set
843e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
844e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
845e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mOnMenuItemClickListener = listener;
846e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
847e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
84876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
84976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Set the content insets for this toolbar relative to layout direction.
85076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
85176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
85276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
85376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
85476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
85576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetStart Content inset for the toolbar starting edge
85676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetEnd Content inset for the toolbar ending edge
85776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
85876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
85976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
86076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
86176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
86276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
86376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
86476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) {
86576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
86676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
86776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
86876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
86976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the starting content inset for this toolbar.
87076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
87176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
87276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
87376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
87476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
87576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The starting content inset for this toolbar
87676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
87776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
87876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
87976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
88076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
88176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
88276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
88376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetStart() {
88476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getStart();
88576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
88676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
88776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
88876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the ending content inset for this toolbar.
88976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
89076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
89176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
89276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
89376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
89476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The ending content inset for this toolbar
89576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
89676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
89776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
89876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
89976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
90076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
90176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
90276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetEnd() {
90376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getEnd();
90476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
90576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
90676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
90776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Set the content insets for this toolbar.
90876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
90976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
91076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
91176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
91276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
91376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetLeft Content inset for the toolbar's left edge
91476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetRight Content inset for the toolbar's right edge
91576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
91676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
91776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
91876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
91976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
92076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
92176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
92276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) {
92376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
92476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
92576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
92676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
92776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the left content inset for this toolbar.
92876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
92976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
93076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
93176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
93276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
93376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The left content inset for this toolbar
93476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
93576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
93676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
93776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
93876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
93976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
94076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
94176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetLeft() {
94276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getLeft();
94376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
94476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
94576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
94676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * Get the right content inset for this toolbar.
94776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
94876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
94976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
95076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
95176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
95276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The right content inset for this toolbar
95376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
95476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
95576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
95676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
95776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
95876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
95976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
96076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetRight() {
96176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getRight();
96276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
96376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
964e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void ensureNavButtonView() {
965e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView == null) {
966e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mNavButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
967e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
968e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
969e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mNavButtonView.setLayoutParams(lp);
970e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
971e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
972e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
973e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureCollapseButtonView() {
974e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mCollapseButtonView == null) {
975e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
976e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setImageDrawable(mCollapseIcon);
977e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
978e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
979e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.mViewType = LayoutParams.EXPANDED;
980e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setLayoutParams(lp);
981e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setOnClickListener(new OnClickListener() {
982e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                @Override
983e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                public void onClick(View v) {
984e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    collapseActionView();
985e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
986e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            });
987e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
988e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
989e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
990e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void addSystemView(View v) {
991e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
992e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                LayoutParams.WRAP_CONTENT);
993e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        lp.mViewType = LayoutParams.SYSTEM;
994e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addView(v, lp);
995e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
996e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
997e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
998e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected Parcelable onSaveInstanceState() {
999e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        SavedState state = new SavedState(super.onSaveInstanceState());
1000e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return state;
1001e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1002e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1003e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1004e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onRestoreInstanceState(Parcelable state) {
1005e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final SavedState ss = (SavedState) state;
1006e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        super.onRestoreInstanceState(ss.getSuperState());
1007e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1008e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1009eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    /**
1010eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell     * @hide
1011eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell     */
1012eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    @Override
1013eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    protected void onSetLayoutParams(View child, ViewGroup.LayoutParams lp) {
1014eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        /*
1015eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * Apps may set ActionBar.LayoutParams on their action bar custom views when
1016eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * a Toolbar is actually acting in the role of the action bar. Perform a quick
1017eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * switch with Toolbar.LayoutParams whenever this happens. This does leave open
1018eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * one potential gotcha: if an app retains the ActionBar.LayoutParams reference
1019eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * and attempts to keep making changes to it before layout those changes won't
1020eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * be reflected in the final results.
1021eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         */
1022eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        if (!checkLayoutParams(lp)) {
1023eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell            child.setLayoutParams(generateLayoutParams(lp));
1024eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        }
1025eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    }
1026eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell
1027e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void measureChildConstrained(View child, int parentWidthSpec, int widthUsed,
1028e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            int parentHeightSpec, int heightUsed, int heightConstraint) {
1029e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
1030e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1031e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        int childWidthSpec = getChildMeasureSpec(parentWidthSpec,
1032e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
1033e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        + widthUsed, lp.width);
1034e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        int childHeightSpec = getChildMeasureSpec(parentHeightSpec,
1035e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
1036e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        + heightUsed, lp.height);
1037e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1038e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int childHeightMode = MeasureSpec.getMode(childHeightSpec);
1039e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (childHeightMode != MeasureSpec.EXACTLY && heightConstraint >= 0) {
1040e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final int size = childHeightMode != MeasureSpec.UNSPECIFIED ?
1041e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    Math.min(MeasureSpec.getSize(childHeightSpec), heightConstraint) :
1042e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    heightConstraint;
1043e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childHeightSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
1044e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1045e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        child.measure(childWidthSpec, childHeightSpec);
1046e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1047e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
10487a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    /**
10497a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell     * Returns the width + uncollapsed margins
10507a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell     */
10517a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int measureChildCollapseMargins(View child,
10527a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            int parentWidthMeasureSpec, int widthUsed,
10537a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            int parentHeightMeasureSpec, int heightUsed, int[] collapsingMargins) {
10547a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
10557a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
10567a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int leftDiff = lp.leftMargin - collapsingMargins[0];
10577a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int rightDiff = lp.rightMargin - collapsingMargins[1];
10587a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int leftMargin = Math.max(0, leftDiff);
10597a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int rightMargin = Math.max(0, rightDiff);
10607a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int hMargins = leftMargin + rightMargin;
10617a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, -leftDiff);
10627a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, -rightDiff);
10637a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
10647a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
10657a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                mPaddingLeft + mPaddingRight + hMargins + widthUsed, lp.width);
10667a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
10677a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
10687a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                        + heightUsed, lp.height);
10697a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
10707a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
10717a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        return child.getMeasuredWidth() + hMargins;
10727a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    }
10737a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1074360c1d89227570510de7bb11195c986300488464Adam Powell    /**
1075360c1d89227570510de7bb11195c986300488464Adam Powell     * Returns true if the Toolbar is collapsible and has no child views with a measured size > 0.
1076360c1d89227570510de7bb11195c986300488464Adam Powell     */
1077360c1d89227570510de7bb11195c986300488464Adam Powell    private boolean shouldCollapse() {
1078360c1d89227570510de7bb11195c986300488464Adam Powell        if (!mCollapsible) return false;
1079360c1d89227570510de7bb11195c986300488464Adam Powell
1080360c1d89227570510de7bb11195c986300488464Adam Powell        final int childCount = getChildCount();
1081360c1d89227570510de7bb11195c986300488464Adam Powell        for (int i = 0; i < childCount; i++) {
1082360c1d89227570510de7bb11195c986300488464Adam Powell            final View child = getChildAt(i);
1083360c1d89227570510de7bb11195c986300488464Adam Powell            if (shouldLayout(child) && child.getMeasuredWidth() > 0 &&
1084360c1d89227570510de7bb11195c986300488464Adam Powell                    child.getMeasuredHeight() > 0) {
1085360c1d89227570510de7bb11195c986300488464Adam Powell                return false;
1086360c1d89227570510de7bb11195c986300488464Adam Powell            }
1087360c1d89227570510de7bb11195c986300488464Adam Powell        }
1088360c1d89227570510de7bb11195c986300488464Adam Powell        return true;
1089360c1d89227570510de7bb11195c986300488464Adam Powell    }
1090360c1d89227570510de7bb11195c986300488464Adam Powell
1091e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1092e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1093e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int width = 0;
1094e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int height = 0;
1095e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int childState = 0;
1096e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
10977a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int[] collapsingMargins = mTempMargins;
10987a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int marginStartIndex;
10997a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int marginEndIndex;
11007a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        if (isLayoutRtl()) {
11017a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginStartIndex = 1;
11027a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginEndIndex = 0;
11037a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        } else {
11047a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginStartIndex = 0;
11057a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginEndIndex = 1;
11067a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        }
11077a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1108e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // System views measure first.
1109e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
111076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        int navWidth = 0;
1111e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mNavButtonView)) {
1112e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mNavButtonView, widthMeasureSpec, width, heightMeasureSpec, 0,
1113e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    mMaxButtonHeight);
111476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            navWidth = mNavButtonView.getMeasuredWidth() + getHorizontalMargins(mNavButtonView);
1115e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mNavButtonView.getMeasuredHeight() +
1116e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mNavButtonView));
1117e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mNavButtonView.getMeasuredState());
1118e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1119e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1120e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mCollapseButtonView)) {
1121e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mCollapseButtonView, widthMeasureSpec, width,
1122e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    heightMeasureSpec, 0, mMaxButtonHeight);
1123e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            navWidth = mCollapseButtonView.getMeasuredWidth() +
1124e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getHorizontalMargins(mCollapseButtonView);
1125e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            height = Math.max(height, mCollapseButtonView.getMeasuredHeight() +
1126e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getVerticalMargins(mCollapseButtonView));
1127e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childState = combineMeasuredStates(childState, mCollapseButtonView.getMeasuredState());
1128e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1129e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
11307a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int contentInsetStart = getContentInsetStart();
11317a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        width += Math.max(contentInsetStart, navWidth);
11327a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[marginStartIndex] = Math.max(0, contentInsetStart - navWidth);
113376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
113476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        int menuWidth = 0;
1135e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mMenuView)) {
1136e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mMenuView, widthMeasureSpec, width, heightMeasureSpec, 0,
1137e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    mMaxButtonHeight);
113876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            menuWidth = mMenuView.getMeasuredWidth() + getHorizontalMargins(mMenuView);
1139e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mMenuView.getMeasuredHeight() +
1140e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mMenuView));
1141e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mMenuView.getMeasuredState());
1142e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1143e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
11447a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int contentInsetEnd = getContentInsetEnd();
11457a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        width += Math.max(contentInsetEnd, menuWidth);
11467a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[marginEndIndex] = Math.max(0, contentInsetEnd - menuWidth);
114776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
1148e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mExpandedActionView)) {
11497a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += measureChildCollapseMargins(mExpandedActionView, widthMeasureSpec, width,
11507a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, 0, collapsingMargins);
1151e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            height = Math.max(height, mExpandedActionView.getMeasuredHeight() +
1152e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getVerticalMargins(mExpandedActionView));
1153e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childState = combineMeasuredStates(childState, mExpandedActionView.getMeasuredState());
1154e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1155e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1156e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mLogoView)) {
11577a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += measureChildCollapseMargins(mLogoView, widthMeasureSpec, width,
11587a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, 0, collapsingMargins);
1159e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mLogoView.getMeasuredHeight() +
1160e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mLogoView));
1161e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mLogoView.getMeasuredState());
1162e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1163e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1164b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        final int childCount = getChildCount();
1165b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        for (int i = 0; i < childCount; i++) {
1166b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            final View child = getChildAt(i);
1167b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1168b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            if (lp.mViewType != LayoutParams.CUSTOM || !shouldLayout(child)) {
1169b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                // We already got all system views above. Skip them and GONE views.
1170b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                continue;
1171b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            }
1172b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell
1173b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            width += measureChildCollapseMargins(child, widthMeasureSpec, width,
1174b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                    heightMeasureSpec, 0, collapsingMargins);
1175b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            height = Math.max(height, child.getMeasuredHeight() + getVerticalMargins(child));
1176b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            childState = combineMeasuredStates(childState, child.getMeasuredState());
1177b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        }
1178b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell
1179e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleWidth = 0;
1180e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleHeight = 0;
1181e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int titleVertMargins = mTitleMarginTop + mTitleMarginBottom;
1182e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int titleHorizMargins = mTitleMarginStart + mTitleMarginEnd;
1183e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mTitleTextView)) {
11847a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            titleWidth = measureChildCollapseMargins(mTitleTextView, widthMeasureSpec,
11857a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    width + titleHorizMargins, heightMeasureSpec, titleVertMargins,
11867a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    collapsingMargins);
1187e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleWidth = mTitleTextView.getMeasuredWidth() + getHorizontalMargins(mTitleTextView);
1188e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight = mTitleTextView.getMeasuredHeight() + getVerticalMargins(mTitleTextView);
1189e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mTitleTextView.getMeasuredState());
1190e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1191e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mSubtitleTextView)) {
11927a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            titleWidth = Math.max(titleWidth, measureChildCollapseMargins(mSubtitleTextView,
11937a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    widthMeasureSpec, width + titleHorizMargins,
11947a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, titleHeight + titleVertMargins,
11957a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    collapsingMargins));
1196e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight += mSubtitleTextView.getMeasuredHeight() +
1197e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mSubtitleTextView);
1198e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mSubtitleTextView.getMeasuredState());
1199e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1200e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1201e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        width += titleWidth;
1202e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        height = Math.max(height, titleHeight);
1203e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1204e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Measurement already took padding into account for available space for the children,
1205e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // add it in for the final size.
1206e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        width += getPaddingLeft() + getPaddingRight();
1207e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        height += getPaddingTop() + getPaddingBottom();
1208e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1209e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int measuredWidth = resolveSizeAndState(
1210e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                Math.max(width, getSuggestedMinimumWidth()),
1211e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                widthMeasureSpec, childState & MEASURED_STATE_MASK);
1212e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int measuredHeight = resolveSizeAndState(
1213e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                Math.max(height, getSuggestedMinimumHeight()),
1214e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT);
1215360c1d89227570510de7bb11195c986300488464Adam Powell
1216360c1d89227570510de7bb11195c986300488464Adam Powell        setMeasuredDimension(measuredWidth, shouldCollapse() ? 0 : measuredHeight);
1217e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1218e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1219e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1220e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
1221e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
1222e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int width = getWidth();
1223e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int height = getHeight();
1224e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingLeft = getPaddingLeft();
1225e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingRight = getPaddingRight();
1226e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingTop = getPaddingTop();
1227e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingBottom = getPaddingBottom();
1228e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int left = paddingLeft;
1229e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int right = width - paddingRight;
1230e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
12317a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int[] collapsingMargins = mTempMargins;
12327a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = collapsingMargins[1] = 0;
12337a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1234e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mNavButtonView)) {
1235e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
12367a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mNavButtonView, right, collapsingMargins);
1237e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
12387a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mNavButtonView, left, collapsingMargins);
1239e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1240e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1241e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1242e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mCollapseButtonView)) {
1243e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (isRtl) {
12447a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mCollapseButtonView, right, collapsingMargins);
1245e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            } else {
12467a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mCollapseButtonView, left, collapsingMargins);
1247e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1248e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1249e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1250e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mMenuView)) {
1251e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
12527a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mMenuView, left, collapsingMargins);
1253e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
12547a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mMenuView, right, collapsingMargins);
1255e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1256e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1257e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
12587a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, getContentInsetLeft() - left);
12597a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, getContentInsetRight() - (width - paddingRight - right));
126076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        left = Math.max(left, getContentInsetLeft());
126176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        right = Math.min(right, width - paddingRight - getContentInsetRight());
126276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
1263e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mExpandedActionView)) {
1264e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (isRtl) {
12657a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mExpandedActionView, right, collapsingMargins);
1266e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            } else {
12677a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mExpandedActionView, left, collapsingMargins);
1268e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1269e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1270e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1271e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mLogoView)) {
1272e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
12737a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right = layoutChildRight(mLogoView, right, collapsingMargins);
1274e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
12757a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left = layoutChildLeft(mLogoView, left, collapsingMargins);
1276e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1277e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1278e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1279e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean layoutTitle = shouldLayout(mTitleTextView);
1280e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean layoutSubtitle = shouldLayout(mSubtitleTextView);
1281e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleHeight = 0;
1282e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutTitle) {
1283e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1284e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight += lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
1285e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1286e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutSubtitle) {
1287e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1288b3e411458cd7b96bb308e2de3086efc4da10d64dAdam Powell            titleHeight += lp.topMargin + mSubtitleTextView.getMeasuredHeight() + lp.bottomMargin;
1289e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1290e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1291e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutTitle || layoutSubtitle) {
1292e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            int titleTop;
1293e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View topChild = layoutTitle ? mTitleTextView : mSubtitleTextView;
1294e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View bottomChild = layoutSubtitle ? mSubtitleTextView : mTitleTextView;
1295e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams toplp = (LayoutParams) topChild.getLayoutParams();
1296e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams bottomlp = (LayoutParams) bottomChild.getLayoutParams();
1297e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1298e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) {
1299e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.TOP:
1300e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    titleTop = getPaddingTop() + toplp.topMargin + mTitleMarginTop;
1301e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1302e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                default:
1303e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.CENTER_VERTICAL:
1304e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int space = height - paddingTop - paddingBottom;
1305e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    int spaceAbove = (space - titleHeight) / 2;
1306e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    if (spaceAbove < toplp.topMargin + mTitleMarginTop) {
1307e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        spaceAbove = toplp.topMargin + mTitleMarginTop;
1308e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    } else {
1309e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        final int spaceBelow = height - paddingBottom - titleHeight -
1310e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                                spaceAbove - paddingTop;
1311e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) {
1312e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                            spaceAbove = Math.max(0, spaceAbove -
1313e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                                    (bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow));
1314e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        }
1315e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
1316e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = paddingTop + spaceAbove;
1317e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1318e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.BOTTOM:
1319e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    titleTop = height - paddingBottom - bottomlp.bottomMargin - mTitleMarginBottom -
1320e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            titleHeight;
1321e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1322e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1323e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
13247a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                final int rd = mTitleMarginStart - collapsingMargins[1];
13257a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right -= Math.max(0, rd);
13267a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                collapsingMargins[1] = Math.max(0, -rd);
1327e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int titleRight = right;
1328e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int subtitleRight = right;
13297a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1330e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutTitle) {
1331e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1332e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleLeft = titleRight - mTitleTextView.getMeasuredWidth();
1333e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
1334e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
13357a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    titleRight = titleLeft - mTitleMarginEnd;
1336e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = titleBottom + lp.bottomMargin;
1337e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1338e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutSubtitle) {
1339e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1340e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop += lp.topMargin;
1341e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleLeft = subtitleRight - mSubtitleTextView.getMeasuredWidth();
1342e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
1343e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
13447a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    subtitleRight = subtitleRight - mTitleMarginEnd;
1345e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = subtitleBottom + lp.bottomMargin;
1346e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1347c80f95228560e5912e97886ed637f4d538e87c70Adam Powell                right = Math.min(titleRight, subtitleRight);
1348e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
13497a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                final int ld = mTitleMarginStart - collapsingMargins[0];
13507a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left += Math.max(0, ld);
13517a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                collapsingMargins[0] = Math.max(0, -ld);
1352e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int titleLeft = left;
1353e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int subtitleLeft = left;
13547a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1355e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutTitle) {
1356e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1357e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleRight = titleLeft + mTitleTextView.getMeasuredWidth();
1358e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
1359e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
13607a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    titleLeft = titleRight + mTitleMarginEnd;
1361e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = titleBottom + lp.bottomMargin;
1362e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1363e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutSubtitle) {
1364e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1365e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop += lp.topMargin;
1366e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleRight = subtitleLeft + mSubtitleTextView.getMeasuredWidth();
1367e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
1368e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
13697a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    subtitleLeft = subtitleRight + mTitleMarginEnd;
1370e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = subtitleBottom + lp.bottomMargin;
1371e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1372e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                left = Math.max(titleLeft, subtitleLeft);
1373e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1374e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1375e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1376e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Get all remaining children sorted for layout. This is all prepared
1377e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // such that absolute layout direction can be used below.
1378e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1379e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.LEFT);
1380e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int leftViewsCount = mTempViews.size();
1381e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < leftViewsCount; i++) {
13827a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            left = layoutChildLeft(mTempViews.get(i), left, collapsingMargins);
1383e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1384e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1385e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.RIGHT);
1386e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int rightViewsCount = mTempViews.size();
1387e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < rightViewsCount; i++) {
13887a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            right = layoutChildRight(mTempViews.get(i), right, collapsingMargins);
1389e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1390e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1391e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Centered views try to center with respect to the whole bar, but views pinned
1392e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // to the left or right can push the mass of centered views to one side or the other.
1393e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.CENTER_HORIZONTAL);
13947a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int centerViewsWidth = getViewListMeasuredWidth(mTempViews, collapsingMargins);
1395e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2;
1396e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int halfCenterViewsWidth = centerViewsWidth / 2;
1397e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int centerLeft = parentCenter - halfCenterViewsWidth;
1398e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int centerRight = centerLeft + centerViewsWidth;
1399e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (centerLeft < left) {
1400e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            centerLeft = left;
1401e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (centerRight > right) {
1402e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            centerLeft -= centerRight - right;
1403e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1404e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1405e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int centerViewsCount = mTempViews.size();
1406e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < centerViewsCount; i++) {
14077a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            centerLeft = layoutChildLeft(mTempViews.get(i), centerLeft, collapsingMargins);
1408e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1409e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTempViews.clear();
1410e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1411e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
14127a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int getViewListMeasuredWidth(List<View> views, int[] collapsingMargins) {
14137a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        int collapseLeft = collapsingMargins[0];
14147a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        int collapseRight = collapsingMargins[1];
1415e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int width = 0;
1416e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int count = views.size();
1417e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < count; i++) {
1418e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final View v = views.get(i);
1419e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) v.getLayoutParams();
14207a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int l = lp.leftMargin - collapseLeft;
14217a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int r = lp.rightMargin - collapseRight;
14227a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int leftMargin = Math.max(0, l);
14237a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int rightMargin = Math.max(0, r);
14247a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            collapseLeft = Math.max(0, -l);
14257a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            collapseRight = Math.max(0, -r);
14267a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += leftMargin + v.getMeasuredWidth() + rightMargin;
1427e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1428e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return width;
1429e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1430e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
14317a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int layoutChildLeft(View child, int left, int[] collapsingMargins) {
1432e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
14337a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int l = lp.leftMargin - collapsingMargins[0];
14347a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        left += Math.max(0, l);
14357a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, -l);
14367a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int top = getChildTop(child);
14377a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int childWidth = child.getMeasuredWidth();
14387a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        child.layout(left, top, left + childWidth, top + child.getMeasuredHeight());
14397a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        left += childWidth + lp.rightMargin;
1440e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return left;
1441e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1442e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
14437a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int layoutChildRight(View child, int right, int[] collapsingMargins) {
1444e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
14457a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int r = lp.rightMargin - collapsingMargins[1];
14467a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        right -= Math.max(0, r);
14477a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, -r);
14487a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int top = getChildTop(child);
14497a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int childWidth = child.getMeasuredWidth();
14507a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        child.layout(right - childWidth, top, right, top + child.getMeasuredHeight());
14517a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        right -= childWidth + lp.leftMargin;
1452e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return right;
1453e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1454e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1455e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildTop(View child) {
1456e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1457e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (getChildVerticalGravity(lp.gravity)) {
1458e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.TOP:
1459e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return getPaddingTop();
1460e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1461e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.BOTTOM:
1462e002c2fb39f9df1db38a7412832882ae622660d4Adam Powell                return getHeight() - getPaddingBottom() -
1463e002c2fb39f9df1db38a7412832882ae622660d4Adam Powell                        child.getMeasuredHeight() - lp.bottomMargin;
1464e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1465e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1466e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_VERTICAL:
1467e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int paddingTop = getPaddingTop();
1468e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int paddingBottom = getPaddingBottom();
1469e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int height = getHeight();
1470e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int childHeight = child.getMeasuredHeight();
1471e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int space = height - paddingTop - paddingBottom;
1472e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int spaceAbove = (space - childHeight) / 2;
1473e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (spaceAbove < lp.topMargin) {
1474e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    spaceAbove = lp.topMargin;
1475e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                } else {
1476e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int spaceBelow = height - paddingBottom - childHeight -
1477e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                            spaceAbove - paddingTop;
1478e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    if (spaceBelow < lp.bottomMargin) {
1479e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        spaceAbove = Math.max(0, spaceAbove - (lp.bottomMargin - spaceBelow));
1480e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
1481e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1482e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return paddingTop + spaceAbove;
1483e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1484e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1485e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1486e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildVerticalGravity(int gravity) {
1487e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int vgrav = gravity & Gravity.VERTICAL_GRAVITY_MASK;
1488e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (vgrav) {
1489e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.TOP:
1490e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.BOTTOM:
1491e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_VERTICAL:
1492e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return vgrav;
1493e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1494e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return mGravity & Gravity.VERTICAL_GRAVITY_MASK;
1495e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1496e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1497e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1498e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1499e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Prepare a list of non-SYSTEM child views. If the layout direction is RTL
1500e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * this will be in reverse child order.
1501e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1502e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param views List to populate. It will be cleared before use.
1503e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param gravity Horizontal gravity to match against
1504e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1505e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void addCustomViewsWithGravity(List<View> views, int gravity) {
1506e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
1507e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int childCount = getChildCount();
1508e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int absGrav = Gravity.getAbsoluteGravity(gravity, getLayoutDirection());
1509e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1510e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        views.clear();
1511e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1512e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (isRtl) {
1513e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            for (int i = childCount - 1; i >= 0; i--) {
1514e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final View child = getChildAt(i);
1515e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1516e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
1517e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        getChildHorizontalGravity(lp.gravity) == absGrav) {
1518e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    views.add(child);
1519e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1520e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1521e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else {
1522e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            for (int i = 0; i < childCount; i++) {
1523e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final View child = getChildAt(i);
1524e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1525e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
1526e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        getChildHorizontalGravity(lp.gravity) == absGrav) {
1527e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    views.add(child);
1528e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1529e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1530e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1531e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1532e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1533e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildHorizontalGravity(int gravity) {
1534e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int ld = getLayoutDirection();
1535e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int absGrav = Gravity.getAbsoluteGravity(gravity, ld);
1536e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int hGrav = absGrav & Gravity.HORIZONTAL_GRAVITY_MASK;
1537e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (hGrav) {
1538e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.LEFT:
1539e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.RIGHT:
1540e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_HORIZONTAL:
1541e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return hGrav;
1542e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1543e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return ld == LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT;
1544e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1545e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1546e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1547e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private boolean shouldLayout(View view) {
1548e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return view != null && view.getParent() == this && view.getVisibility() != GONE;
1549e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1550e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1551e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getHorizontalMargins(View v) {
1552e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
1553e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mlp.getMarginStart() + mlp.getMarginEnd();
1554e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1555e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1556e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getVerticalMargins(View v) {
1557e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
1558e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mlp.topMargin + mlp.bottomMargin;
1559e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1560e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1561e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1562e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public LayoutParams generateLayoutParams(AttributeSet attrs) {
1563e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return new LayoutParams(getContext(), attrs);
1564e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1565e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1566e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1567e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1568e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (p instanceof LayoutParams) {
1569e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams((LayoutParams) p);
1570e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        } else if (p instanceof ActionBar.LayoutParams) {
1571e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return new LayoutParams((ActionBar.LayoutParams) p);
1572e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (p instanceof MarginLayoutParams) {
1573e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams((MarginLayoutParams) p);
1574e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else {
1575e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams(p);
1576e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1577e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1578e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1579e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1580e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    protected LayoutParams generateDefaultLayoutParams() {
1581e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
1582e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1583e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1584e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1585e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1586e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return super.checkLayoutParams(p) && p instanceof LayoutParams;
1587e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1588e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1589e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private static boolean isCustomView(View child) {
1590e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return ((LayoutParams) child.getLayoutParams()).mViewType == LayoutParams.CUSTOM;
1591e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1592e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1593e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
1594e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public DecorToolbar getWrapper() {
1595e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mWrapper == null) {
1596e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mWrapper = new ToolbarWidgetWrapper(this);
1597e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1598e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mWrapper;
1599e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1600e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1601e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void setChildVisibilityForExpandedActionView(boolean expand) {
1602e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int childCount = getChildCount();
1603e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        for (int i = 0; i < childCount; i++) {
1604e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View child = getChildAt(i);
1605e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1606e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (lp.mViewType != LayoutParams.EXPANDED && child != mMenuView) {
1607e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                child.setVisibility(expand ? GONE : VISIBLE);
1608e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1609e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1610e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1611e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1612e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1613360c1d89227570510de7bb11195c986300488464Adam Powell     * Force the toolbar to collapse to zero-height during measurement if
1614360c1d89227570510de7bb11195c986300488464Adam Powell     * it could be considered "empty" (no visible elements with nonzero measured size)
1615360c1d89227570510de7bb11195c986300488464Adam Powell     * @hide
1616360c1d89227570510de7bb11195c986300488464Adam Powell     */
1617360c1d89227570510de7bb11195c986300488464Adam Powell    public void setCollapsible(boolean collapsible) {
1618360c1d89227570510de7bb11195c986300488464Adam Powell        mCollapsible = collapsible;
1619360c1d89227570510de7bb11195c986300488464Adam Powell        requestLayout();
1620360c1d89227570510de7bb11195c986300488464Adam Powell    }
1621360c1d89227570510de7bb11195c986300488464Adam Powell
1622360c1d89227570510de7bb11195c986300488464Adam Powell    /**
1623e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Interface responsible for receiving menu item click events if the items themselves
1624e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * do not have individual item click listeners.
1625e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1626e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public interface OnMenuItemClickListener {
1627e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        /**
1628e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * This method will be invoked when a menu item is clicked if the item itself did
1629e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * not already handle the event.
1630e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         *
1631e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * @param item {@link MenuItem} that was clicked
1632e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * @return <code>true</code> if the event was handled, <code>false</code> otherwise.
1633e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         */
1634e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public boolean onMenuItemClick(MenuItem item);
1635e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1636e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1637e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1638e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Layout information for child views of Toolbars.
1639e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1640d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * <p>Toolbar.LayoutParams extends ActionBar.LayoutParams for compatibility with existing
1641d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * ActionBar API. See {@link android.app.Activity#setActionBar(Toolbar) Activity.setActionBar}
1642d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * for more info on how to use a Toolbar as your Activity's ActionBar.</p>
1643d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     *
1644e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @attr ref android.R.styleable#Toolbar_LayoutParams_layout_gravity
1645e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1646e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public static class LayoutParams extends ActionBar.LayoutParams {
1647e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        static final int CUSTOM = 0;
1648e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        static final int SYSTEM = 1;
1649e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        static final int EXPANDED = 2;
1650e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1651e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int mViewType = CUSTOM;
1652e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1653e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(@NonNull Context c, AttributeSet attrs) {
1654e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(c, attrs);
1655e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1656e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1657e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int width, int height) {
1658e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(width, height);
1659e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this.gravity = Gravity.CENTER_VERTICAL | Gravity.START;
1660e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1661e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1662e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int width, int height, int gravity) {
1663e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(width, height);
1664e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this.gravity = gravity;
1665e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1666e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1667e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int gravity) {
1668e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this(WRAP_CONTENT, MATCH_PARENT, gravity);
1669e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1670e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1671e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(LayoutParams source) {
1672e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1673e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1674e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mViewType = source.mViewType;
1675e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1676e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1677e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public LayoutParams(ActionBar.LayoutParams source) {
1678e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            super(source);
1679e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1680e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1681e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(MarginLayoutParams source) {
1682e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1683d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            // ActionBar.LayoutParams doesn't have a MarginLayoutParams constructor.
1684d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            // Fake it here and copy over the relevant data.
1685d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            copyMarginsFrom(source);
1686e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1687e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1688e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(ViewGroup.LayoutParams source) {
1689e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1690e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1691e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1692e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1693e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    static class SavedState extends BaseSavedState {
1694e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public SavedState(Parcel source) {
1695e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
1696e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1697e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1698e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public SavedState(Parcelable superState) {
1699e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(superState);
1700e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1701e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1702e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        @Override
1703e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public void writeToParcel(Parcel out, int flags) {
1704e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super.writeToParcel(out, flags);
1705e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1706e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1707e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
1708e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1709e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            @Override
1710e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            public SavedState createFromParcel(Parcel source) {
1711e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return new SavedState(source);
1712e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1713e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1714e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            @Override
1715e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            public SavedState[] newArray(int size) {
1716e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return new SavedState[size];
1717e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1718e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        };
1719e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1720e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1721e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private class ExpandedActionViewMenuPresenter implements MenuPresenter {
1722e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        MenuBuilder mMenu;
1723e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        MenuItemImpl mCurrentExpandedItem;
1724e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1725e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1726e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void initForMenu(Context context, MenuBuilder menu) {
1727e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Clear the expanded action view when menus change.
1728e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mMenu != null && mCurrentExpandedItem != null) {
1729e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mMenu.collapseItemActionView(mCurrentExpandedItem);
1730e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1731e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenu = menu;
1732e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1733e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1734e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1735e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public MenuView getMenuView(ViewGroup root) {
1736e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return null;
1737e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1738e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1739e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1740e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void updateMenuView(boolean cleared) {
1741e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Make sure the expanded item we have is still there.
1742e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mCurrentExpandedItem != null) {
1743e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                boolean found = false;
1744e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1745e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (mMenu != null) {
1746e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    final int count = mMenu.size();
1747e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    for (int i = 0; i < count; i++) {
1748e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        final MenuItem item = mMenu.getItem(i);
1749e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        if (item == mCurrentExpandedItem) {
1750e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            found = true;
1751e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            break;
1752e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        }
1753e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    }
1754e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
1755e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1756e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (!found) {
1757e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    // The item we had expanded disappeared. Collapse.
1758e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    collapseItemActionView(mMenu, mCurrentExpandedItem);
1759e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
1760e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1761e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1762e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1763e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1764e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void setCallback(Callback cb) {
1765e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1766e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1767e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1768e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
1769e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
1770e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1771e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1772e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1773e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
1774e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1775e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1776e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1777e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean flagActionItems() {
1778e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
1779e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1780e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1781e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1782e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
1783e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureCollapseButtonView();
1784e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mCollapseButtonView.getParent() != Toolbar.this) {
1785e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                addView(mCollapseButtonView);
1786e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1787e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedActionView = item.getActionView();
1788e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCurrentExpandedItem = item;
1789e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView.getParent() != Toolbar.this) {
1790e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                final LayoutParams lp = generateDefaultLayoutParams();
1791e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
1792e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                lp.mViewType = LayoutParams.EXPANDED;
1793e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedActionView.setLayoutParams(lp);
1794e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                addView(mExpandedActionView);
1795e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1796e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1797e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            setChildVisibilityForExpandedActionView(true);
1798e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            requestLayout();
1799e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.setActionViewExpanded(true);
1800e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1801e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView instanceof CollapsibleActionView) {
1802e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                ((CollapsibleActionView) mExpandedActionView).onActionViewExpanded();
1803e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1804e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1805e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return true;
1806e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1807e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1808e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1809e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
1810e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Do this before detaching the actionview from the hierarchy, in case
1811e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // it needs to dismiss the soft keyboard, etc.
1812e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView instanceof CollapsibleActionView) {
1813e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                ((CollapsibleActionView) mExpandedActionView).onActionViewCollapsed();
1814e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1815e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1816e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            removeView(mExpandedActionView);
1817e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            removeView(mCollapseButtonView);
1818e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedActionView = null;
1819e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1820e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            setChildVisibilityForExpandedActionView(false);
1821e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCurrentExpandedItem = null;
1822e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            requestLayout();
1823e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.setActionViewExpanded(false);
1824e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1825e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return true;
1826e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1827e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1828e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1829e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public int getId() {
1830e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return 0;
1831e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1832e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1833e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1834e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public Parcelable onSaveInstanceState() {
1835e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return null;
1836e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1837e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1838e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
1839e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void onRestoreInstanceState(Parcelable state) {
1840e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1841e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1842e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell}
1843