Toolbar.java revision 67960a0a555808eaf512454f0a344c672c398292
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
1980756e38882720860db52f1fcc21fa1505a02abfTor Norbyeimport android.annotation.ColorInt;
20417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbyeimport android.annotation.DrawableRes;
21417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbyeimport android.annotation.MenuRes;
22e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.annotation.NonNull;
23e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viveretteimport android.annotation.Nullable;
24417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbyeimport android.annotation.StringRes;
25417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbyeimport android.annotation.StyleRes;
2667960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikovimport android.annotation.TestApi;
27e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport android.app.ActionBar;
28e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.content.Context;
29e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.content.res.TypedArray;
30e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.graphics.drawable.Drawable;
31e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.os.Parcel;
32e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.os.Parcelable;
33e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport android.text.Layout;
34e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.text.TextUtils;
35e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.util.AttributeSet;
36e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport android.view.CollapsibleActionView;
373d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viveretteimport android.view.ContextThemeWrapper;
38e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.Gravity;
39e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.Menu;
40e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.MenuInflater;
41e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.MenuItem;
4261d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powellimport android.view.MotionEvent;
43e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.View;
44e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport android.view.ViewGroup;
453d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
46e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport com.android.internal.R;
47e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuBuilder;
48e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuItemImpl;
49e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuPresenter;
50e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.MenuView;
51e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.view.menu.SubMenuBuilder;
52e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.widget.DecorToolbar;
53e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powellimport com.android.internal.widget.ToolbarWidgetWrapper;
54e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
55e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport java.util.ArrayList;
56e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellimport java.util.List;
57e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
58e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell/**
59e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * A standard toolbar for use within application content.
60e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
61e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <p>A Toolbar is a generalization of {@link android.app.ActionBar action bars} for use
62e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * within application layouts. While an action bar is traditionally part of an
63e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * {@link android.app.Activity Activity's} opaque window decor controlled by the framework,
64e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy.
65e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * An application may choose to designate a Toolbar as the action bar for an Activity
66e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * using the {@link android.app.Activity#setActionBar(Toolbar) setActionBar()} method.</p>
67e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
68e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <p>Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar
69e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * may contain a combination of the following optional elements:
70e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
71e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <ul>
72e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>A navigation button.</em> This may be an Up arrow, navigation menu toggle, close,
73e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     collapse, done or another glyph of the app's choosing. This button should always be used
74e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     to access other navigational destinations within the container of the Toolbar and
75157afde397f817e0a3a21c5d47011db462b6fff6Alan Viverette *     its signified content or otherwise leave the current context signified by the Toolbar.
76157afde397f817e0a3a21c5d47011db462b6fff6Alan Viverette *     The navigation button is vertically aligned within the Toolbar's
77157afde397f817e0a3a21c5d47011db462b6fff6Alan Viverette *     {@link android.R.styleable#View_minHeight minimum height}, if set.</li>
78e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>A branded logo image.</em> This may extend to the height of the bar and can be
79e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     arbitrarily wide.</li>
80e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>A title and subtitle.</em> The title should be a signpost for the Toolbar's current
81e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     position in the navigation hierarchy and the content contained there. The subtitle,
82e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     if present should indicate any extended information about the current content.
83e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     If an app uses a logo image it should strongly consider omitting a title and subtitle.</li>
84e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>One or more custom views.</em> The application may add arbitrary child views
85e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     to the Toolbar. They will appear at this position within the layout. If a child view's
86e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     {@link LayoutParams} indicates a {@link Gravity} value of
87e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     {@link Gravity#CENTER_HORIZONTAL CENTER_HORIZONTAL} the view will attempt to center
88e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     within the available space remaining in the Toolbar after all other elements have been
89e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     measured.</li>
90e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <li><em>An {@link ActionMenuView action menu}.</em> The menu of actions will pin to the
91e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     end of the Toolbar offering a few
92e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *     <a href="http://developer.android.com/design/patterns/actionbar.html#ActionButtons">
93157afde397f817e0a3a21c5d47011db462b6fff6Alan Viverette *     frequent, important or typical</a> actions along with an optional overflow menu for
94157afde397f817e0a3a21c5d47011db462b6fff6Alan Viverette *     additional actions. Action buttons are vertically aligned within the Toolbar's
95157afde397f817e0a3a21c5d47011db462b6fff6Alan Viverette *     {@link android.R.styleable#View_minHeight minimum height}, if set.</li>
96e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * </ul>
97e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * </p>
98e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell *
99e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * <p>In modern Android UIs developers should lean more on a visually distinct color scheme for
100e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * toolbars than on their application icon. The use of application icon plus title as a standard
101e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell * layout is discouraged on API 21 devices and newer.</p>
1024db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette *
1034db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_buttonGravity
1044db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_collapseContentDescription
1054db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_collapseIcon
1064db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_contentInsetEnd
1074db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_contentInsetLeft
1084db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_contentInsetRight
1094db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_contentInsetStart
110679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell * @attr ref android.R.styleable#Toolbar_contentInsetStartWithNavigation
111679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell * @attr ref android.R.styleable#Toolbar_contentInsetEndWithActions
1124db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_gravity
1134db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_logo
1144db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_logoDescription
1154db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_maxButtonHeight
1164db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_navigationContentDescription
1174db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_navigationIcon
1184db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_popupTheme
1194db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_subtitle
1204db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_subtitleTextAppearance
1214db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_subtitleTextColor
1224db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_title
1234db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_titleMargin
1244db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_titleMarginBottom
1254db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_titleMarginEnd
1264db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_titleMarginStart
1274db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_titleMarginTop
1284db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_titleTextAppearance
1294db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette * @attr ref android.R.styleable#Toolbar_titleTextColor
130e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell */
131e43340c80dc66c45edc793ecd0343774aa34d108Adam Powellpublic class Toolbar extends ViewGroup {
132e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private static final String TAG = "Toolbar";
133e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
134e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private ActionMenuView mMenuView;
135e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private TextView mTitleTextView;
136e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private TextView mSubtitleTextView;
137e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private ImageButton mNavButtonView;
138e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private ImageView mLogoView;
139e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
140e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private Drawable mCollapseIcon;
14185085343b378f00a34448b69e582226fa584b005Alan Viverette    private CharSequence mCollapseDescription;
142e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ImageButton mCollapseButtonView;
143e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    View mExpandedActionView;
144e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1453d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /** Context against which to inflate popup menus. */
1463d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    private Context mPopupContext;
1473d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
1483d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /** Theme resource against which to inflate popup menus. */
1493d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    private int mPopupTheme;
1503d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
151e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleTextAppearance;
152e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mSubtitleTextAppearance;
153e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private int mNavButtonStyle;
154e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
155e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private int mButtonGravity;
156e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
157e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private int mMaxButtonHeight;
15876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
159e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginStart;
160e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginEnd;
161e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginTop;
162e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mTitleMarginBottom;
163e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
16476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    private final RtlSpacingHelper mContentInsets = new RtlSpacingHelper();
165679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    private int mContentInsetStartWithNavigation;
166679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    private int mContentInsetEndWithActions;
16776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
168e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int mGravity = Gravity.START | Gravity.CENTER_VERTICAL;
169e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
170e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private CharSequence mTitleText;
171e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private CharSequence mSubtitleText;
172e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1731a264ce3d915b187b71101436bec3328c76288b0Adam Powell    private int mTitleTextColor;
1741a264ce3d915b187b71101436bec3328c76288b0Adam Powell    private int mSubtitleTextColor;
1751a264ce3d915b187b71101436bec3328c76288b0Adam Powell
17661d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell    private boolean mEatingTouch;
17761d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell
178e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    // Clear me after use.
179e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private final ArrayList<View> mTempViews = new ArrayList<View>();
180e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1815f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell    // Used to hold views that will be removed while we have an expanded action view.
1825f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell    private final ArrayList<View> mHiddenViews = new ArrayList<>();
1835f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell
1847a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private final int[] mTempMargins = new int[2];
1857a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
186e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private OnMenuItemClickListener mOnMenuItemClickListener;
187e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
188e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private final ActionMenuView.OnMenuItemClickListener mMenuViewItemClickListener =
189e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            new ActionMenuView.OnMenuItemClickListener() {
190e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                @Override
191e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                public boolean onMenuItemClick(MenuItem item) {
192e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    if (mOnMenuItemClickListener != null) {
193e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        return mOnMenuItemClickListener.onMenuItemClick(item);
194e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
195e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    return false;
196e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
197e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            };
198e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
199e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ToolbarWidgetWrapper mWrapper;
200e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ActionMenuPresenter mOuterActionMenuPresenter;
201e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private ExpandedActionViewMenuPresenter mExpandedMenuPresenter;
20204c0d4607897157f01c908475fa0c6d7301cf7e5Adam Powell    private MenuPresenter.Callback mActionMenuPresenterCallback;
203c46125042493e66f7dfdccec24e3b0739600b194Adam Powell    private MenuBuilder.Callback mMenuBuilderCallback;
204e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
205360c1d89227570510de7bb11195c986300488464Adam Powell    private boolean mCollapsible;
206360c1d89227570510de7bb11195c986300488464Adam Powell
20731c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    private final Runnable mShowOverflowMenuRunnable = new Runnable() {
20831c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        @Override public void run() {
20931c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            showOverflowMenu();
21031c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        }
21131c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    };
21231c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
213e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context) {
214e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        this(context, null);
215e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
216e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
217e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context, AttributeSet attrs) {
218e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        this(context, attrs, com.android.internal.R.attr.toolbarStyle);
219e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
220e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
221e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context, AttributeSet attrs, int defStyleAttr) {
222e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        this(context, attrs, defStyleAttr, 0);
223e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
224e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
225e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Toolbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
226e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        super(context, attrs, defStyleAttr, defStyleRes);
227e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
228e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Toolbar,
229e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                defStyleAttr, defStyleRes);
230e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
231e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTitleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
232e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mSubtitleTextAppearance = a.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, 0);
233e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mNavButtonStyle = a.getResourceId(R.styleable.Toolbar_navigationButtonStyle, 0);
234e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mGravity = a.getInteger(R.styleable.Toolbar_gravity, mGravity);
235e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);
23676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mTitleMarginStart = mTitleMarginEnd = mTitleMarginTop = mTitleMarginBottom =
2374db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette                a.getDimensionPixelOffset(R.styleable.Toolbar_titleMargin, 0);
238e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
239e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginStart = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginStart, -1);
240e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginStart >= 0) {
241e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginStart = marginStart;
242e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
243e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
244e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginEnd = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginEnd, -1);
245e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginEnd >= 0) {
246e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginEnd = marginEnd;
247e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
248e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
249e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginTop = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginTop, -1);
250e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginTop >= 0) {
251e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginTop = marginTop;
252e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
253e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
254e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int marginBottom = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginBottom,
255e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                -1);
256e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (marginBottom >= 0) {
257e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleMarginBottom = marginBottom;
258e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
259e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
260e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mMaxButtonHeight = a.getDimensionPixelSize(R.styleable.Toolbar_maxButtonHeight, -1);
261e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
26276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetStart =
26376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetStart,
26476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                        RtlSpacingHelper.UNDEFINED);
26576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetEnd =
26676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetEnd,
26776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                        RtlSpacingHelper.UNDEFINED);
26876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetLeft =
26976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetLeft, 0);
27076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        final int contentInsetRight =
27176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0);
27276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
27376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
27476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
27576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        if (contentInsetStart != RtlSpacingHelper.UNDEFINED ||
27676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell                contentInsetEnd != RtlSpacingHelper.UNDEFINED) {
27776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
27876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        }
27976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
280679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        mContentInsetStartWithNavigation = a.getDimensionPixelOffset(
281679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                R.styleable.Toolbar_contentInsetStartWithNavigation, RtlSpacingHelper.UNDEFINED);
282679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        mContentInsetEndWithActions = a.getDimensionPixelOffset(
283679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                R.styleable.Toolbar_contentInsetEndWithActions, RtlSpacingHelper.UNDEFINED);
284679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
285e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mCollapseIcon = a.getDrawable(R.styleable.Toolbar_collapseIcon);
28685085343b378f00a34448b69e582226fa584b005Alan Viverette        mCollapseDescription = a.getText(R.styleable.Toolbar_collapseContentDescription);
287e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
288e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final CharSequence title = a.getText(R.styleable.Toolbar_title);
289e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(title)) {
290e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            setTitle(title);
291e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
292e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
293e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final CharSequence subtitle = a.getText(R.styleable.Toolbar_subtitle);
294e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(subtitle)) {
29507a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            setSubtitle(subtitle);
296e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2973d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
29877e5037d8125b94e524264dc1890ab9b6b7df0afAlan Viverette        // Set the default context, since setPopupTheme() may be a no-op.
29977e5037d8125b94e524264dc1890ab9b6b7df0afAlan Viverette        mPopupContext = mContext;
30022a117d1b60869ed36d125796396f190b4b002fbAlan Viverette        setPopupTheme(a.getResourceId(R.styleable.Toolbar_popupTheme, 0));
3017c13bc2a7090544a04fc93813794a8b97bc86ff7Adam Powell
3027c13bc2a7090544a04fc93813794a8b97bc86ff7Adam Powell        final Drawable navIcon = a.getDrawable(R.styleable.Toolbar_navigationIcon);
3037c13bc2a7090544a04fc93813794a8b97bc86ff7Adam Powell        if (navIcon != null) {
3047c13bc2a7090544a04fc93813794a8b97bc86ff7Adam Powell            setNavigationIcon(navIcon);
30512b0321c0b400f105cad40568a1f572020ba6336Adam Powell        }
30612b0321c0b400f105cad40568a1f572020ba6336Adam Powell
30712b0321c0b400f105cad40568a1f572020ba6336Adam Powell        final CharSequence navDesc = a.getText(
30812b0321c0b400f105cad40568a1f572020ba6336Adam Powell                R.styleable.Toolbar_navigationContentDescription);
30912b0321c0b400f105cad40568a1f572020ba6336Adam Powell        if (!TextUtils.isEmpty(navDesc)) {
31012b0321c0b400f105cad40568a1f572020ba6336Adam Powell            setNavigationContentDescription(navDesc);
3117c13bc2a7090544a04fc93813794a8b97bc86ff7Adam Powell        }
3126b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell
3136b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        final Drawable logo = a.getDrawable(R.styleable.Toolbar_logo);
3146b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        if (logo != null) {
3156b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell            setLogo(logo);
3166b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        }
3176b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell
3186b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        final CharSequence logoDesc = a.getText(R.styleable.Toolbar_logoDescription);
3196b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        if (!TextUtils.isEmpty(logoDesc)) {
3206b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell            setLogoDescription(logoDesc);
3216b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        }
3226b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell
3236b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        if (a.hasValue(R.styleable.Toolbar_titleTextColor)) {
3246b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell            setTitleTextColor(a.getColor(R.styleable.Toolbar_titleTextColor, 0xffffffff));
3256b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        }
3266b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell
3276b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        if (a.hasValue(R.styleable.Toolbar_subtitleTextColor)) {
3286b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell            setSubtitleTextColor(a.getColor(R.styleable.Toolbar_subtitleTextColor, 0xffffffff));
3296b3fc9a9035c87a23828c8f481732cb3213302a4Adam Powell        }
33022a117d1b60869ed36d125796396f190b4b002fbAlan Viverette        a.recycle();
3313d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    }
3323d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
3333d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /**
3343d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * Specifies the theme to use when inflating popup menus. By default, uses
3353d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * the same theme as the toolbar itself.
3363d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     *
3373d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @param resId theme used to inflate popup menus
3383d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @see #getPopupTheme()
3393d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     */
340417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setPopupTheme(@StyleRes int resId) {
3413d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        if (mPopupTheme != resId) {
3423d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mPopupTheme = resId;
3433d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            if (resId == 0) {
3443d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette                mPopupContext = mContext;
3453d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            } else {
3463d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette                mPopupContext = new ContextThemeWrapper(mContext, resId);
3473d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            }
3483d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        }
3493d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    }
3503d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette
3513d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    /**
3523d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @return resource identifier of the theme used to inflate popup menus, or
3533d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     *         0 if menus are inflated against the toolbar theme
3543d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     * @see #setPopupTheme(int)
3553d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette     */
3563d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette    public int getPopupTheme() {
3573d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        return mPopupTheme;
358e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
359e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
3604db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
3614db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * Sets the title margin.
3624db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     *
3634db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param start the starting title margin in pixels
3644db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param top the top title margin in pixels
3654db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param end the ending title margin in pixels
3664db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param bottom the bottom title margin in pixels
3674db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginStart()
3684db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginTop()
3694db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginEnd()
3704db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginBottom()
3714db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMargin
3724db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
3734db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public void setTitleMargin(int start, int top, int end, int bottom) {
3744db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginStart = start;
3754db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginTop = top;
3764db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginEnd = end;
3774db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginBottom = bottom;
3784db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
3794db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        requestLayout();
3804db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
3814db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
3824db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
3834db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @return the starting title margin in pixels
3844db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #setTitleMarginStart(int)
3854db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginStart
3864db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
3874db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public int getTitleMarginStart() {
3884db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        return mTitleMarginStart;
3894db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
3904db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
3914db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
3924db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * Sets the starting title margin in pixels.
3934db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     *
3944db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param margin the starting title margin in pixels
3954db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginStart()
3964db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginStart
3974db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
3984db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public void setTitleMarginStart(int margin) {
3994db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginStart = margin;
4004db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4014db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        requestLayout();
4024db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
4034db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4044db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
4054db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @return the top title margin in pixels
4064db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #setTitleMarginTop(int)
4074db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginTop
4084db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
4094db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public int getTitleMarginTop() {
4104db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        return mTitleMarginTop;
4114db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
4124db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4134db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
4144db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * Sets the top title margin in pixels.
4154db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     *
4164db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param margin the top title margin in pixels
4174db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginTop()
4184db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginTop
4194db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
4204db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public void setTitleMarginTop(int margin) {
4214db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginTop = margin;
4224db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4234db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        requestLayout();
4244db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
4254db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4264db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
4274db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @return the ending title margin in pixels
4284db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #setTitleMarginEnd(int)
4294db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginEnd
4304db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
4314db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public int getTitleMarginEnd() {
4324db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        return mTitleMarginEnd;
4334db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
4344db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4354db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
4364db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * Sets the ending title margin in pixels.
4374db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     *
4384db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param margin the ending title margin in pixels
4394db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginEnd()
4404db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginEnd
4414db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
4424db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public void setTitleMarginEnd(int margin) {
4434db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginEnd = margin;
4444db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4454db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        requestLayout();
4464db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
4474db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4484db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
4494db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @return the bottom title margin in pixels
4504db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #setTitleMarginBottom(int)
4514db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginBottom
4524db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
4534db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public int getTitleMarginBottom() {
4544db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        return mTitleMarginBottom;
4554db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
4564db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4574db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    /**
4584db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * Sets the bottom title margin in pixels.
4594db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     *
4604db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @param margin the bottom title margin in pixels
4614db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @see #getTitleMarginBottom()
4624db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     * @attr ref android.R.styleable#Toolbar_titleMarginBottom
4634db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette     */
4644db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    public void setTitleMarginBottom(int margin) {
4654db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        mTitleMarginBottom = margin;
4664db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
4674db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette        requestLayout();
4684db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette    }
4694db9d1b48b49ffc43f1c2d46660ca67a88d673f4Alan Viverette
47076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    @Override
47176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) {
47276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        super.onRtlPropertiesChanged(layoutDirection);
47376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setDirection(layoutDirection == LAYOUT_DIRECTION_RTL);
47476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
47576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
476e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
477e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a logo drawable from a resource id.
478e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
479e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This drawable should generally take the place of title text. The logo cannot be
480e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * clicked. Apps using a logo should also supply a description using
481e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setLogoDescription(int)}.</p>
482e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
483e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId ID of a drawable resource
484e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
485417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setLogo(@DrawableRes int resId) {
486e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setLogo(getContext().getDrawable(resId));
487e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
488e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
489e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
490e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean canShowOverflowMenu() {
491e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return getVisibility() == VISIBLE && mMenuView != null && mMenuView.isOverflowReserved();
492e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
493e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
494e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
495e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Check whether the overflow menu is currently showing. This may not reflect
496e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * a pending show operation in progress.
497e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
498e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the overflow menu is currently showing
499e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
500e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isOverflowMenuShowing() {
501e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.isOverflowMenuShowing();
502e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
503e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
504e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
505e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isOverflowMenuShowPending() {
506e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.isOverflowMenuShowPending();
507e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
508e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
509e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
510e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Show the overflow items from the associated menu.
511e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
512e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the menu was able to be shown, false otherwise
513e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
514e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean showOverflowMenu() {
515e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.showOverflowMenu();
516e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
517e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
518e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
519e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Hide the overflow items from the associated menu.
520e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
521e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the menu was able to be hidden, false otherwise
522e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
523e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean hideOverflowMenu() {
524e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mMenuView != null && mMenuView.hideOverflowMenu();
525e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
526e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
527e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
528e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void setMenu(MenuBuilder menu, ActionMenuPresenter outerPresenter) {
529e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (menu == null && mMenuView == null) {
530e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return;
531e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
532e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
533e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        ensureMenuView();
534e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MenuBuilder oldMenu = mMenuView.peekMenu();
535e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (oldMenu == menu) {
536e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return;
537e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
538e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
539e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (oldMenu != null) {
540e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            oldMenu.removeMenuPresenter(mOuterActionMenuPresenter);
541e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            oldMenu.removeMenuPresenter(mExpandedMenuPresenter);
542e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
543e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
544e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mExpandedMenuPresenter == null) {
545e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
546e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
547e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
548e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        outerPresenter.setExpandedActionViewsExclusive(true);
549e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (menu != null) {
5503d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(outerPresenter, mPopupContext);
5513d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(mExpandedMenuPresenter, mPopupContext);
552e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        } else {
5533d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            outerPresenter.initForMenu(mPopupContext, null);
5543d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mExpandedMenuPresenter.initForMenu(mPopupContext, null);
555e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            outerPresenter.updateMenuView(true);
556e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedMenuPresenter.updateMenuView(true);
557e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
5583d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette        mMenuView.setPopupTheme(mPopupTheme);
559e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mMenuView.setPresenter(outerPresenter);
560e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        mOuterActionMenuPresenter = outerPresenter;
561e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
562e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
563e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
564e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Dismiss all currently showing popup menus, including overflow or submenus.
565e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
566e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void dismissPopupMenus() {
567e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mMenuView != null) {
568e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenuView.dismissPopupMenus();
569e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
570e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
571e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
572e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
573e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean isTitleTruncated() {
574e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mTitleTextView == null) {
575e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
576e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
577e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
578e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final Layout titleLayout = mTitleTextView.getLayout();
579e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (titleLayout == null) {
580e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
581e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
582e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
583e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int lineCount = titleLayout.getLineCount();
584e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        for (int i = 0; i < lineCount; i++) {
585e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (titleLayout.getEllipsisCount(i) > 0) {
586e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                return true;
587e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
588e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
589e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return false;
590e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
591e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
592e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
593e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a logo drawable.
594e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
595e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This drawable should generally take the place of title text. The logo cannot be
596e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * clicked. Apps using a logo should also supply a description using
597e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * {@link #setLogoDescription(int)}.</p>
598e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
599e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param drawable Drawable to use as a logo
600e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
601e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogo(Drawable drawable) {
602e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (drawable != null) {
603e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureLogoView();
6045f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            if (!isChildOrHidden(mLogoView)) {
6055f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell                addSystemView(mLogoView, true);
606e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
6075f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        } else if (mLogoView != null && isChildOrHidden(mLogoView)) {
608e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mLogoView);
6095f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            mHiddenViews.remove(mLogoView);
610e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
611e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mLogoView != null) {
612e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mLogoView.setImageDrawable(drawable);
613e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
614e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
615e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
616e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
617e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the current logo drawable.
618e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
619e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The current logo drawable
620e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setLogo(int)
621e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setLogo(android.graphics.drawable.Drawable)
622e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
623e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Drawable getLogo() {
624e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mLogoView != null ? mLogoView.getDrawable() : null;
625e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
626e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
627e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
628e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description of the toolbar's logo.
629e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
630e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description will be used for accessibility or other similar descriptions
631e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * of the UI.</p>
632e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
633e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId String resource id
634e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
635417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setLogoDescription(@StringRes int resId) {
636e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setLogoDescription(getContext().getText(resId));
637e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
638e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
639e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
640e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a description of the toolbar's logo.
641e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
642e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This description will be used for accessibility or other similar descriptions
643e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * of the UI.</p>
644e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
645e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param description Description to set
646e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
647e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setLogoDescription(CharSequence description) {
648e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (!TextUtils.isEmpty(description)) {
649e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureLogoView();
650e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
651e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mLogoView != null) {
652e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mLogoView.setContentDescription(description);
653e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
654e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
655e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
656e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
657e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the description of the toolbar's logo.
658e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
659e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return A description of the logo
660e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
661e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getLogoDescription() {
662e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mLogoView != null ? mLogoView.getContentDescription() : null;
663e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
664e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
665e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureLogoView() {
666e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mLogoView == null) {
667e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mLogoView = new ImageView(getContext());
668e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
669e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
670e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
671e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
672e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Check whether this Toolbar is currently hosting an expanded action view.
673e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
674e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * <p>An action view may be expanded either directly from the
675e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * {@link android.view.MenuItem MenuItem} it belongs to or by user action. If the Toolbar
676e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * has an expanded action view it can be collapsed using the {@link #collapseActionView()}
677e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * method.</p>
678e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
679e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return true if the Toolbar has an expanded action view
680e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
681e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public boolean hasExpandedActionView() {
682e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mExpandedMenuPresenter != null &&
683e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedMenuPresenter.mCurrentExpandedItem != null;
684e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
685e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
686e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
687e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Collapse a currently expanded action view. If this Toolbar does not have an
688e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * expanded action view this method has no effect.
689e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
690e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * <p>An action view may be expanded either directly from the
691e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * {@link android.view.MenuItem MenuItem} it belongs to or by user action.</p>
692e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
693e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @see #hasExpandedActionView()
694e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
695e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public void collapseActionView() {
696e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MenuItemImpl item = mExpandedMenuPresenter == null ? null :
697e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedMenuPresenter.mCurrentExpandedItem;
698e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (item != null) {
699e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.collapseActionView();
700e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
701e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
702e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
703e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
704e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Returns the title of this toolbar.
705e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
706e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * @return The current title.
707e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
708e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getTitle() {
709e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mTitleText;
710e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
711e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
712e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
713e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the title of this toolbar.
714e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
715e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>A title should be used as the anchor for a section of content. It should
716e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * describe or name the content being viewed.</p>
717e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
718e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId Resource ID of a string to set as the title
719e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
720417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setTitle(@StringRes int resId) {
721e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setTitle(getContext().getText(resId));
722e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
723e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
724e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
725e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the title of this toolbar.
726e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
727e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>A title should be used as the anchor for a section of content. It should
728e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * describe or name the content being viewed.</p>
729e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
730e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param title Title to set
731e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
732e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setTitle(CharSequence title) {
733e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(title)) {
734e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mTitleTextView == null) {
735e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final Context context = getContext();
736e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                mTitleTextView = new TextView(context);
737e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mTitleTextView.setSingleLine();
738e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
7391a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mTitleTextAppearance != 0) {
740bb98ebd6b15f4cf942b156892988801c95601f2fAlan Viverette                    mTitleTextView.setTextAppearance(mTitleTextAppearance);
7411a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
7421a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mTitleTextColor != 0) {
7431a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mTitleTextView.setTextColor(mTitleTextColor);
7441a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
745e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
7465f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            if (!isChildOrHidden(mTitleTextView)) {
7475f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell                addSystemView(mTitleTextView, true);
748e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
7495f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        } else if (mTitleTextView != null && isChildOrHidden(mTitleTextView)) {
750e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mTitleTextView);
7515f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            mHiddenViews.remove(mTitleTextView);
752e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
753e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mTitleTextView != null) {
754e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mTitleTextView.setText(title);
755e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
756e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTitleText = title;
757e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
758e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
759e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
760e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the subtitle of this toolbar.
761e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
762e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The current subtitle
763e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
764e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public CharSequence getSubtitle() {
765e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mSubtitleText;
766e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
767e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
768e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
769e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the subtitle of this toolbar.
770e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
771e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Subtitles should express extended information about the current content.</p>
772e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
773e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId String resource ID
774e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
775417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setSubtitle(@StringRes int resId) {
776e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        setSubtitle(getContext().getText(resId));
777e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
778e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
779e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
780e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the subtitle of this toolbar.
781e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
782e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Subtitles should express extended information about the current content.</p>
783e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
784e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param subtitle Subtitle to set
785e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
786e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setSubtitle(CharSequence subtitle) {
787e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (!TextUtils.isEmpty(subtitle)) {
788e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (mSubtitleTextView == null) {
789e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final Context context = getContext();
790e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                mSubtitleTextView = new TextView(context);
791e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mSubtitleTextView.setSingleLine();
792e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mSubtitleTextView.setEllipsize(TextUtils.TruncateAt.END);
7931a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mSubtitleTextAppearance != 0) {
794bb98ebd6b15f4cf942b156892988801c95601f2fAlan Viverette                    mSubtitleTextView.setTextAppearance(mSubtitleTextAppearance);
7951a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
7961a264ce3d915b187b71101436bec3328c76288b0Adam Powell                if (mSubtitleTextColor != 0) {
7971a264ce3d915b187b71101436bec3328c76288b0Adam Powell                    mSubtitleTextView.setTextColor(mSubtitleTextColor);
7981a264ce3d915b187b71101436bec3328c76288b0Adam Powell                }
799e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
8005f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            if (!isChildOrHidden(mSubtitleTextView)) {
8015f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell                addSystemView(mSubtitleTextView, true);
802e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
8035f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        } else if (mSubtitleTextView != null && isChildOrHidden(mSubtitleTextView)) {
804e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mSubtitleTextView);
8055f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            mHiddenViews.remove(mSubtitleTextView);
806e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
807e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mSubtitleTextView != null) {
808e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mSubtitleTextView.setText(subtitle);
809e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
810e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mSubtitleText = subtitle;
811e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
812e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
813e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
814ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * Sets the text color, size, style, hint color, and highlight color
815ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * from the specified TextAppearance resource.
816ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     */
817417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setTitleTextAppearance(Context context, @StyleRes int resId) {
818ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        mTitleTextAppearance = resId;
819ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        if (mTitleTextView != null) {
820bb98ebd6b15f4cf942b156892988801c95601f2fAlan Viverette            mTitleTextView.setTextAppearance(resId);
821ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        }
822ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    }
823ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell
824ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    /**
825ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * Sets the text color, size, style, hint color, and highlight color
826ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     * from the specified TextAppearance resource.
827ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell     */
828417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setSubtitleTextAppearance(Context context, @StyleRes int resId) {
829ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        mSubtitleTextAppearance = resId;
830ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        if (mSubtitleTextView != null) {
831bb98ebd6b15f4cf942b156892988801c95601f2fAlan Viverette            mSubtitleTextView.setTextAppearance(resId);
832ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell        }
833ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    }
834ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell
835ebba5d4edd763b19124b715a0f084d2f00c84a47Adam Powell    /**
8361a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * Sets the text color of the title, if present.
8371a264ce3d915b187b71101436bec3328c76288b0Adam Powell     *
8381a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * @param color The new text color in 0xAARRGGBB format
8391a264ce3d915b187b71101436bec3328c76288b0Adam Powell     */
84080756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setTitleTextColor(@ColorInt int color) {
8411a264ce3d915b187b71101436bec3328c76288b0Adam Powell        mTitleTextColor = color;
8421a264ce3d915b187b71101436bec3328c76288b0Adam Powell        if (mTitleTextView != null) {
8431a264ce3d915b187b71101436bec3328c76288b0Adam Powell            mTitleTextView.setTextColor(color);
8441a264ce3d915b187b71101436bec3328c76288b0Adam Powell        }
8451a264ce3d915b187b71101436bec3328c76288b0Adam Powell    }
8461a264ce3d915b187b71101436bec3328c76288b0Adam Powell
8471a264ce3d915b187b71101436bec3328c76288b0Adam Powell    /**
8481a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * Sets the text color of the subtitle, if present.
8491a264ce3d915b187b71101436bec3328c76288b0Adam Powell     *
8501a264ce3d915b187b71101436bec3328c76288b0Adam Powell     * @param color The new text color in 0xAARRGGBB format
8511a264ce3d915b187b71101436bec3328c76288b0Adam Powell     */
85280756e38882720860db52f1fcc21fa1505a02abfTor Norbye    public void setSubtitleTextColor(@ColorInt int color) {
8531a264ce3d915b187b71101436bec3328c76288b0Adam Powell        mSubtitleTextColor = color;
8541a264ce3d915b187b71101436bec3328c76288b0Adam Powell        if (mSubtitleTextView != null) {
8551a264ce3d915b187b71101436bec3328c76288b0Adam Powell            mSubtitleTextView.setTextColor(color);
8561a264ce3d915b187b71101436bec3328c76288b0Adam Powell        }
8571a264ce3d915b187b71101436bec3328c76288b0Adam Powell    }
8581a264ce3d915b187b71101436bec3328c76288b0Adam Powell
8591a264ce3d915b187b71101436bec3328c76288b0Adam Powell    /**
8607901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * Retrieve the currently configured content description for the navigation button view.
8617901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * This will be used to describe the navigation action to users through mechanisms such
8627901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * as screen readers or tooltips.
8637901b2a54981b769bf27852c41ce612871a8e019Adam Powell     *
8647901b2a54981b769bf27852c41ce612871a8e019Adam Powell     * @return The navigation button's content description
865d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     *
866d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     * @attr ref android.R.styleable#Toolbar_navigationContentDescription
8677901b2a54981b769bf27852c41ce612871a8e019Adam Powell     */
868e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette    @Nullable
8697901b2a54981b769bf27852c41ce612871a8e019Adam Powell    public CharSequence getNavigationContentDescription() {
8707901b2a54981b769bf27852c41ce612871a8e019Adam Powell        return mNavButtonView != null ? mNavButtonView.getContentDescription() : null;
8717901b2a54981b769bf27852c41ce612871a8e019Adam Powell    }
8727901b2a54981b769bf27852c41ce612871a8e019Adam Powell
8737901b2a54981b769bf27852c41ce612871a8e019Adam Powell    /**
874e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Set a content description for the navigation button if one is present. The content
875e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * description will be read via screen readers or other accessibility systems to explain
876e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * the action of the navigation button.
877e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
878e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * @param resId Resource ID of a content description string to set, or 0 to
879e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     *              clear the description
880d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     *
881d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     * @attr ref android.R.styleable#Toolbar_navigationContentDescription
882e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
883417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setNavigationContentDescription(@StringRes int resId) {
884e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette        setNavigationContentDescription(resId != 0 ? getContext().getText(resId) : null);
885e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
886e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
887e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
888e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * Set a content description for the navigation button if one is present. The content
889e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * description will be read via screen readers or other accessibility systems to explain
890e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     * the action of the navigation button.
891e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     *
892e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * @param description Content description to set, or <code>null</code> to
893e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     *                    clear the content description
894d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     *
895d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     * @attr ref android.R.styleable#Toolbar_navigationContentDescription
896e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell     */
897e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette    public void setNavigationContentDescription(@Nullable CharSequence description) {
898e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette        if (!TextUtils.isEmpty(description)) {
899e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette            ensureNavButtonView();
900e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette        }
901e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette        if (mNavButtonView != null) {
902e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette            mNavButtonView.setContentDescription(description);
903e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette        }
904e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette    }
905e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette
906e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette    /**
907e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * Set the icon to use for the toolbar's navigation button.
908e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     *
909e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * <p>The navigation button appears at the start of the toolbar if present. Setting an icon
910e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * will make the navigation button visible.</p>
911e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     *
912e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * <p>If you use a navigation icon you should also set a description for its action using
913e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * {@link #setNavigationContentDescription(int)}. This is used for accessibility and
914e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * tooltips.</p>
915e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     *
916e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * @param resId Resource ID of a drawable to set
917d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     *
918d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     * @attr ref android.R.styleable#Toolbar_navigationIcon
919e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     */
920417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void setNavigationIcon(@DrawableRes int resId) {
921e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette        setNavigationIcon(getContext().getDrawable(resId));
922e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
923e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
924e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /**
925e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set the icon to use for the toolbar's navigation button.
926e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
927e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>The navigation button appears at the start of the toolbar if present. Setting an icon
928e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * will make the navigation button visible.</p>
929e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
930e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>If you use a navigation icon you should also set a description for its action using
931e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * {@link #setNavigationContentDescription(int)}. This is used for accessibility and
932e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * tooltips.</p>
933e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
934e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette     * @param icon Drawable to set, may be null to clear the icon
935d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     *
936d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     * @attr ref android.R.styleable#Toolbar_navigationIcon
937e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
938e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette    public void setNavigationIcon(@Nullable Drawable icon) {
939e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (icon != null) {
940e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            ensureNavButtonView();
9415f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            if (!isChildOrHidden(mNavButtonView)) {
9425f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell                addSystemView(mNavButtonView, true);
943e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
9445f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        } else if (mNavButtonView != null && isChildOrHidden(mNavButtonView)) {
945e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            removeView(mNavButtonView);
9465f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            mHiddenViews.remove(mNavButtonView);
947e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
948e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView != null) {
949e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mNavButtonView.setImageDrawable(icon);
950e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
951e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
952e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
953e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
954e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Return the current drawable used as the navigation icon.
955e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
956e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @return The navigation icon drawable
957d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     *
958d760249623a57d35dc0b4e57972eb9a72934d500Adam Powell     * @attr ref android.R.styleable#Toolbar_navigationIcon
959e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
960e88700a2ad69e9b99824f2de3d8ba3d44054b24cAlan Viverette    @Nullable
961e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public Drawable getNavigationIcon() {
962e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mNavButtonView != null ? mNavButtonView.getDrawable() : null;
963e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
964e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
965e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
966e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a listener to respond to navigation events.
967e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
968e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This listener will be called whenever the user clicks the navigation button
969e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * at the start of the toolbar. An icon must be set for the navigation button to appear.</p>
970e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
971e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param listener Listener to set
972e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @see #setNavigationIcon(android.graphics.drawable.Drawable)
973e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
974e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setNavigationOnClickListener(OnClickListener listener) {
975e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        ensureNavButtonView();
976e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mNavButtonView.setOnClickListener(listener);
977e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
978e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
979e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
98067960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov     * @hide
98167960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov     */
98267960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov    @Nullable
98367960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov    @TestApi
98467960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov    public View getNavigationView() {
98567960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov        return mNavButtonView;
98667960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov    }
98767960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov
98867960a0a555808eaf512454f0a344c672c398292Kirill Grouchnikov    /**
989a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * Return the Menu shown in the toolbar.
9909cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes     *
991a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * <p>Applications that wish to populate the toolbar's menu can do so from here. To use
992a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * an XML menu resource, use {@link #inflateMenu(int)}.</p>
9939cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes     *
994a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * @return The toolbar's Menu
9959cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes     */
996a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes    public Menu getMenu() {
997a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes        ensureMenu();
998a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes        return mMenuView.getMenu();
9999cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes    }
10009cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes
10019cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes    /**
1002a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * Set the icon to use for the overflow button.
10039cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes     *
1004a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * @param icon Drawable to set, may be null to clear the icon
10059cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes     */
1006a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes    public void setOverflowIcon(@Nullable Drawable icon) {
1007a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes        ensureMenu();
1008a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes        mMenuView.setOverflowIcon(icon);
10099cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes    }
10109cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes
10119cc36ca1b520c06f882e84b909f3a2b75d303a06Chris Banes    /**
1012a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * Return the current drawable used as the overflow icon.
1013e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1014a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes     * @return The overflow icon drawable
1015e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1016a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes    @Nullable
1017a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes    public Drawable getOverflowIcon() {
101807a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        ensureMenu();
1019a41b78973db3dfde159f7d641f794f462ad323d3Chris Banes        return mMenuView.getOverflowIcon();
1020e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1021e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
102207a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell    private void ensureMenu() {
102307a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        ensureMenuView();
102407a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        if (mMenuView.peekMenu() == null) {
102507a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            // Initialize a new menu for the first time.
102607a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            final MenuBuilder menu = (MenuBuilder) mMenuView.getMenu();
102707a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            if (mExpandedMenuPresenter == null) {
102807a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell                mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
102907a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            }
103007a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell            mMenuView.setExpandedActionViewsExclusive(true);
10313d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            menu.addMenuPresenter(mExpandedMenuPresenter, mPopupContext);
103207a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell        }
103307a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell    }
103407a74548ae5c1e064508cb1c79ac34de1142b240Adam Powell
1035e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureMenuView() {
1036e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mMenuView == null) {
1037e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mMenuView = new ActionMenuView(getContext());
10383d0f21dab8d891b9aebdd5277348d549eeb843e6Alan Viverette            mMenuView.setPopupTheme(mPopupTheme);
1039e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            mMenuView.setOnMenuItemClickListener(mMenuViewItemClickListener);
1040c46125042493e66f7dfdccec24e3b0739600b194Adam Powell            mMenuView.setMenuCallbacks(mActionMenuPresenterCallback, mMenuBuilderCallback);
1041e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
1042e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.END | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
1043e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenuView.setLayoutParams(lp);
10445f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            addSystemView(mMenuView, false);
1045e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1046e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1047e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1048e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private MenuInflater getMenuInflater() {
1049e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return new MenuInflater(getContext());
1050e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1051e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1052e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1053e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Inflate a menu resource into this toolbar.
1054e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1055e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>Inflate an XML menu resource into this toolbar. Existing items in the menu will not
1056e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * be modified or removed.</p>
1057e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1058e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param resId ID of a menu resource to inflate
1059e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1060417ee5ba89f7e0fe5efd34fce74bf1ee5923d976Tor Norbye    public void inflateMenu(@MenuRes int resId) {
1061e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        getMenuInflater().inflate(resId, getMenu());
1062e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1063e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1064e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1065e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Set a listener to respond to menu item click events.
1066e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1067e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * <p>This listener will be invoked whenever a user selects a menu item from
1068e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * the action buttons presented at the end of the toolbar or the associated overflow.</p>
1069e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1070e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param listener Listener to set
1071e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1072e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
1073e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mOnMenuItemClickListener = listener;
1074e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1075e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
107676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
1077679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Sets the content insets for this toolbar relative to layout direction.
107876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
107976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
108076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
108176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
108276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
108376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetStart Content inset for the toolbar starting edge
108476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetEnd Content inset for the toolbar ending edge
108576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
108676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
108776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
108876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
108976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
109076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
1091679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetEnd
1092679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetStart
109376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
109476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) {
109576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
109676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
109776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
109876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
1099679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the starting content inset for this toolbar.
110076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
110176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
110276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
110376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
110476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
110576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The starting content inset for this toolbar
110676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
110776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
110876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
110976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
111076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
111176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
1112679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetStart
111376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
111476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetStart() {
111576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getStart();
111676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
111776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
111876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
1119679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the ending content inset for this toolbar.
112076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
112176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
112276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
112376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
112476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
112576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The ending content inset for this toolbar
112676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
112776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
112876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
112976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
113076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
113176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
1132679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetEnd
113376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
113476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetEnd() {
113576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getEnd();
113676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
113776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
113876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
1139679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Sets the content insets for this toolbar.
114076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
114176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
114276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
114376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
114476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
114576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetLeft Content inset for the toolbar's left edge
114676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @param contentInsetRight Content inset for the toolbar's right edge
114776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
114876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
114976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
115076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
115176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
115276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
1153679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetLeft
1154679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetRight
115576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
115676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) {
115776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
115876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
115976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
116076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
1161679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the left content inset for this toolbar.
116276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
116376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
116476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
116576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
116676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
116776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The left content inset for this toolbar
116876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
116976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
117076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
117176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
117276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
117376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetRight()
1174679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetLeft
117576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
117676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetLeft() {
117776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getLeft();
117876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
117976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
118076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    /**
1181679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the right content inset for this toolbar.
118276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
118376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * <p>The content inset affects the valid area for Toolbar content other than
118476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * the navigation button and menu. Insets define the minimum margin for these components
118576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * and can be used to effectively align Toolbar content along well-known gridlines.</p>
118676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
118776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @return The right content inset for this toolbar
118876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     *
118976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsRelative(int, int)
119076d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #setContentInsetsAbsolute(int, int)
119176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetStart()
119276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetEnd()
119376d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     * @see #getContentInsetLeft()
1194679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetRight
119576d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell     */
119676d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    public int getContentInsetRight() {
119776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        return mContentInsets.getRight();
119876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell    }
119976d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
1200679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1201679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the start content inset to use when a navigation button is present.
1202679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1203679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * <p>Different content insets are often called for when additional buttons are present
1204679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * in the toolbar, as well as at different toolbar sizes. The larger value of
1205679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * {@link #getContentInsetStart()} and this value will be used during layout.</p>
1206679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1207679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @return the start content inset used when a navigation icon has been set in pixels
1208679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1209679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #setContentInsetStartWithNavigation(int)
1210679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetStartWithNavigation
1211679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1212679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public int getContentInsetStartWithNavigation() {
1213679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        return mContentInsetStartWithNavigation != RtlSpacingHelper.UNDEFINED
1214679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                ? mContentInsetStartWithNavigation
1215679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                : getContentInsetStart();
1216679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1217679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1218679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1219679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Sets the start content inset to use when a navigation button is present.
1220679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1221679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * <p>Different content insets are often called for when additional buttons are present
1222679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * in the toolbar, as well as at different toolbar sizes. The larger value of
1223679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * {@link #getContentInsetStart()} and this value will be used during layout.</p>
1224679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1225679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @param insetStartWithNavigation the inset to use when a navigation icon has been set
1226679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *                                 in pixels
1227679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1228679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #getContentInsetStartWithNavigation()
1229679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetStartWithNavigation
1230679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1231679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public void setContentInsetStartWithNavigation(int insetStartWithNavigation) {
1232679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        if (insetStartWithNavigation < 0) {
1233679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            insetStartWithNavigation = RtlSpacingHelper.UNDEFINED;
1234679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        }
1235679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        if (insetStartWithNavigation != mContentInsetStartWithNavigation) {
1236679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            mContentInsetStartWithNavigation = insetStartWithNavigation;
1237679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            if (getNavigationIcon() != null) {
1238679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                requestLayout();
1239679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            }
1240679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        }
1241679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1242679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1243679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1244679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the end content inset to use when action buttons are present.
1245679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1246679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * <p>Different content insets are often called for when additional buttons are present
1247679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * in the toolbar, as well as at different toolbar sizes. The larger value of
1248679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * {@link #getContentInsetEnd()} and this value will be used during layout.</p>
1249679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1250679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @return the end content inset used when a menu has been set in pixels
1251679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1252679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #setContentInsetEndWithActions(int)
1253679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetEndWithActions
1254679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1255679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public int getContentInsetEndWithActions() {
1256679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        return mContentInsetEndWithActions != RtlSpacingHelper.UNDEFINED
1257679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                ? mContentInsetEndWithActions
1258679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                : getContentInsetEnd();
1259679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1260679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1261679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1262679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Sets the start content inset to use when action buttons are present.
1263679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1264679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * <p>Different content insets are often called for when additional buttons are present
1265679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * in the toolbar, as well as at different toolbar sizes. The larger value of
1266679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * {@link #getContentInsetEnd()} and this value will be used during layout.</p>
1267679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1268679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @param insetEndWithActions the inset to use when a menu has been set in pixels
1269679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1270679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #setContentInsetEndWithActions(int)
1271679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @attr ref android.R.styleable#Toolbar_contentInsetEndWithActions
1272679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1273679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public void setContentInsetEndWithActions(int insetEndWithActions) {
1274679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        if (insetEndWithActions < 0) {
1275679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            insetEndWithActions = RtlSpacingHelper.UNDEFINED;
1276679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        }
1277679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        if (insetEndWithActions != mContentInsetEndWithActions) {
1278679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            mContentInsetEndWithActions = insetEndWithActions;
1279679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            if (getNavigationIcon() != null) {
1280679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                requestLayout();
1281679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            }
1282679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        }
1283679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1284679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1285679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1286679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the content inset that will be used on the starting side of the bar in the current
1287679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * toolbar configuration.
1288679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1289679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @return the current content inset start in pixels
1290679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1291679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #getContentInsetStartWithNavigation()
1292679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1293679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public int getCurrentContentInsetStart() {
1294679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        return getNavigationIcon() != null
1295679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                ? Math.max(getContentInsetStart(), Math.max(mContentInsetStartWithNavigation, 0))
1296679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                : getContentInsetStart();
1297679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1298679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1299679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1300679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the content inset that will be used on the ending side of the bar in the current
1301679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * toolbar configuration.
1302679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1303679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @return the current content inset end in pixels
1304679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1305679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #getContentInsetEndWithActions()
1306679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1307679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public int getCurrentContentInsetEnd() {
1308679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        boolean hasActions = false;
1309679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        if (mMenuView != null) {
1310679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            final MenuBuilder mb = mMenuView.peekMenu();
1311679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell            hasActions = mb != null && mb.hasVisibleItems();
1312679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        }
1313679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        return hasActions
1314679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                ? Math.max(getContentInsetEnd(), Math.max(mContentInsetEndWithActions, 0))
1315679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                : getContentInsetEnd();
1316679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1317679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1318679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1319679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the content inset that will be used on the left side of the bar in the current
1320679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * toolbar configuration.
1321679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1322679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @return the current content inset left in pixels
1323679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1324679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #getContentInsetStartWithNavigation()
1325679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #getContentInsetEndWithActions()
1326679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1327679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public int getCurrentContentInsetLeft() {
1328679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        return isLayoutRtl()
1329679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                ? getCurrentContentInsetEnd()
1330679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                : getCurrentContentInsetStart();
1331679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1332679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1333679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    /**
1334679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * Gets the content inset that will be used on the right side of the bar in the current
1335679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * toolbar configuration.
1336679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1337679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @return the current content inset right in pixels
1338679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     *
1339679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #getContentInsetStartWithNavigation()
1340679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     * @see #getContentInsetEndWithActions()
1341679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell     */
1342679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    public int getCurrentContentInsetRight() {
1343679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        return isLayoutRtl()
1344679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                ? getCurrentContentInsetStart()
1345679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell                : getCurrentContentInsetEnd();
1346679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell    }
1347679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell
1348e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void ensureNavButtonView() {
1349e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (mNavButtonView == null) {
1350e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mNavButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
1351e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
1352e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
1353e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mNavButtonView.setLayoutParams(lp);
1354e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1355e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1356e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1357e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void ensureCollapseButtonView() {
1358e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mCollapseButtonView == null) {
1359e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
1360e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setImageDrawable(mCollapseIcon);
136185085343b378f00a34448b69e582226fa584b005Alan Viverette            mCollapseButtonView.setContentDescription(mCollapseDescription);
1362e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = generateDefaultLayoutParams();
1363e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
1364e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            lp.mViewType = LayoutParams.EXPANDED;
1365e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setLayoutParams(lp);
1366e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCollapseButtonView.setOnClickListener(new OnClickListener() {
1367e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                @Override
1368e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                public void onClick(View v) {
1369e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    collapseActionView();
1370e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
1371e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            });
1372e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1373e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1374e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
13755f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell    private void addSystemView(View v, boolean allowHide) {
1376e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        final ViewGroup.LayoutParams vlp = v.getLayoutParams();
1377e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        final LayoutParams lp;
1378e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        if (vlp == null) {
1379e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            lp = generateDefaultLayoutParams();
1380e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        } else if (!checkLayoutParams(vlp)) {
1381e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            lp = generateLayoutParams(vlp);
1382e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        } else {
1383e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            lp = (LayoutParams) vlp;
1384e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        }
1385e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        lp.mViewType = LayoutParams.SYSTEM;
13865f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell
13875f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        if (allowHide && mExpandedActionView != null) {
13885f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            v.setLayoutParams(lp);
13895f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            mHiddenViews.add(v);
13905f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        } else {
13915f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            addView(v, lp);
13925f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        }
1393e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1394e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1395e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1396e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected Parcelable onSaveInstanceState() {
1397e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        SavedState state = new SavedState(super.onSaveInstanceState());
139831c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
139931c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        if (mExpandedMenuPresenter != null && mExpandedMenuPresenter.mCurrentExpandedItem != null) {
140031c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            state.expandedMenuItemId = mExpandedMenuPresenter.mCurrentExpandedItem.getItemId();
140131c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        }
140231c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
140331c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        state.isOverflowOpen = isOverflowMenuShowing();
140431c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
1405e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return state;
1406e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1407e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1408e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1409e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onRestoreInstanceState(Parcelable state) {
1410e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final SavedState ss = (SavedState) state;
1411e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        super.onRestoreInstanceState(ss.getSuperState());
141231c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
141331c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        final Menu menu = mMenuView != null ? mMenuView.peekMenu() : null;
141431c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        if (ss.expandedMenuItemId != 0 && mExpandedMenuPresenter != null && menu != null) {
141531c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            final MenuItem item = menu.findItem(ss.expandedMenuItemId);
141631c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            if (item != null) {
141731c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell                item.expandActionView();
141831c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            }
141931c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        }
142031c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
142131c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        if (ss.isOverflowOpen) {
142231c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            postShowOverflowMenu();
142331c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        }
142431c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    }
142531c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
142631c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    private void postShowOverflowMenu() {
142731c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        removeCallbacks(mShowOverflowMenuRunnable);
142831c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        post(mShowOverflowMenuRunnable);
142931c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    }
143031c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
143131c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    @Override
143231c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    protected void onDetachedFromWindow() {
143331c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        super.onDetachedFromWindow();
143431c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        removeCallbacks(mShowOverflowMenuRunnable);
1435e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1436e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
143761d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell    @Override
143861d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell    public boolean onTouchEvent(MotionEvent ev) {
143961d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        // Toolbars always eat touch events, but should still respect the touch event dispatch
144061d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        // contract. If the normal View implementation doesn't want the events, we'll just silently
144161d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        // eat the rest of the gesture without reporting the events to the default implementation
144261d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        // since that's what it expects.
144361d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell
144461d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        final int action = ev.getActionMasked();
144561d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        if (action == MotionEvent.ACTION_DOWN) {
144661d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell            mEatingTouch = false;
144761d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        }
144861d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell
144961d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        if (!mEatingTouch) {
145061d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell            final boolean handled = super.onTouchEvent(ev);
145161d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell            if (action == MotionEvent.ACTION_DOWN && !handled) {
145261d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell                mEatingTouch = true;
145361d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell            }
145461d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        }
145561d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell
145661d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
145761d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell            mEatingTouch = false;
145861d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        }
145961d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell
146061d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell        return true;
146161d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell    }
146261d15b98561ab500ccaec5d21d98c8c0ea3ae47fAdam Powell
1463eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    /**
1464eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell     * @hide
1465eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell     */
1466eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    @Override
1467eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    protected void onSetLayoutParams(View child, ViewGroup.LayoutParams lp) {
1468eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        /*
1469eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * Apps may set ActionBar.LayoutParams on their action bar custom views when
1470eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * a Toolbar is actually acting in the role of the action bar. Perform a quick
1471eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * switch with Toolbar.LayoutParams whenever this happens. This does leave open
1472eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * one potential gotcha: if an app retains the ActionBar.LayoutParams reference
1473eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * and attempts to keep making changes to it before layout those changes won't
1474eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         * be reflected in the final results.
1475eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell         */
1476eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        if (!checkLayoutParams(lp)) {
1477eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell            child.setLayoutParams(generateLayoutParams(lp));
1478eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell        }
1479eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell    }
1480eb97f473fc075c91760c2b657a7014681cd6f3ceAdam Powell
1481e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private void measureChildConstrained(View child, int parentWidthSpec, int widthUsed,
1482e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            int parentHeightSpec, int heightUsed, int heightConstraint) {
1483e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
1484e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1485e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        int childWidthSpec = getChildMeasureSpec(parentWidthSpec,
1486e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
1487e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        + widthUsed, lp.width);
1488e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        int childHeightSpec = getChildMeasureSpec(parentHeightSpec,
1489e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
1490e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        + heightUsed, lp.height);
1491e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1492e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int childHeightMode = MeasureSpec.getMode(childHeightSpec);
1493e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (childHeightMode != MeasureSpec.EXACTLY && heightConstraint >= 0) {
1494e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final int size = childHeightMode != MeasureSpec.UNSPECIFIED ?
1495e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    Math.min(MeasureSpec.getSize(childHeightSpec), heightConstraint) :
1496e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    heightConstraint;
1497e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childHeightSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
1498e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1499e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        child.measure(childWidthSpec, childHeightSpec);
1500e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
1501e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
15027a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    /**
15037a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell     * Returns the width + uncollapsed margins
15047a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell     */
15057a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int measureChildCollapseMargins(View child,
15067a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            int parentWidthMeasureSpec, int widthUsed,
15077a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            int parentHeightMeasureSpec, int heightUsed, int[] collapsingMargins) {
15087a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
15097a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
15107a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int leftDiff = lp.leftMargin - collapsingMargins[0];
15117a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int rightDiff = lp.rightMargin - collapsingMargins[1];
15127a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int leftMargin = Math.max(0, leftDiff);
15137a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int rightMargin = Math.max(0, rightDiff);
15147a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int hMargins = leftMargin + rightMargin;
15157a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, -leftDiff);
15167a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, -rightDiff);
15177a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
15187a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
15197a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                mPaddingLeft + mPaddingRight + hMargins + widthUsed, lp.width);
15207a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
15217a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
15227a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                        + heightUsed, lp.height);
15237a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
15247a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
15257a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        return child.getMeasuredWidth() + hMargins;
15267a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    }
15277a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1528360c1d89227570510de7bb11195c986300488464Adam Powell    /**
1529360c1d89227570510de7bb11195c986300488464Adam Powell     * Returns true if the Toolbar is collapsible and has no child views with a measured size > 0.
1530360c1d89227570510de7bb11195c986300488464Adam Powell     */
1531360c1d89227570510de7bb11195c986300488464Adam Powell    private boolean shouldCollapse() {
1532360c1d89227570510de7bb11195c986300488464Adam Powell        if (!mCollapsible) return false;
1533360c1d89227570510de7bb11195c986300488464Adam Powell
1534360c1d89227570510de7bb11195c986300488464Adam Powell        final int childCount = getChildCount();
1535360c1d89227570510de7bb11195c986300488464Adam Powell        for (int i = 0; i < childCount; i++) {
1536360c1d89227570510de7bb11195c986300488464Adam Powell            final View child = getChildAt(i);
1537360c1d89227570510de7bb11195c986300488464Adam Powell            if (shouldLayout(child) && child.getMeasuredWidth() > 0 &&
1538360c1d89227570510de7bb11195c986300488464Adam Powell                    child.getMeasuredHeight() > 0) {
1539360c1d89227570510de7bb11195c986300488464Adam Powell                return false;
1540360c1d89227570510de7bb11195c986300488464Adam Powell            }
1541360c1d89227570510de7bb11195c986300488464Adam Powell        }
1542360c1d89227570510de7bb11195c986300488464Adam Powell        return true;
1543360c1d89227570510de7bb11195c986300488464Adam Powell    }
1544360c1d89227570510de7bb11195c986300488464Adam Powell
1545e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1546e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1547e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int width = 0;
1548e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int height = 0;
1549e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int childState = 0;
1550e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
15517a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int[] collapsingMargins = mTempMargins;
15527a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int marginStartIndex;
15537a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int marginEndIndex;
15547a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        if (isLayoutRtl()) {
15557a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginStartIndex = 1;
15567a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginEndIndex = 0;
15577a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        } else {
15587a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginStartIndex = 0;
15597a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            marginEndIndex = 1;
15607a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        }
15617a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1562e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // System views measure first.
1563e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
156476d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        int navWidth = 0;
1565e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mNavButtonView)) {
1566e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mNavButtonView, widthMeasureSpec, width, heightMeasureSpec, 0,
1567e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    mMaxButtonHeight);
156876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            navWidth = mNavButtonView.getMeasuredWidth() + getHorizontalMargins(mNavButtonView);
1569e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mNavButtonView.getMeasuredHeight() +
1570e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mNavButtonView));
1571e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mNavButtonView.getMeasuredState());
1572e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1573e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1574e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mCollapseButtonView)) {
1575e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mCollapseButtonView, widthMeasureSpec, width,
1576e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    heightMeasureSpec, 0, mMaxButtonHeight);
1577e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            navWidth = mCollapseButtonView.getMeasuredWidth() +
1578e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getHorizontalMargins(mCollapseButtonView);
1579e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            height = Math.max(height, mCollapseButtonView.getMeasuredHeight() +
1580e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getVerticalMargins(mCollapseButtonView));
1581e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childState = combineMeasuredStates(childState, mCollapseButtonView.getMeasuredState());
1582e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1583e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1584679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        final int contentInsetStart = getCurrentContentInsetStart();
15857a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        width += Math.max(contentInsetStart, navWidth);
15867a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[marginStartIndex] = Math.max(0, contentInsetStart - navWidth);
158776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
158876d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell        int menuWidth = 0;
1589e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mMenuView)) {
1590e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            measureChildConstrained(mMenuView, widthMeasureSpec, width, heightMeasureSpec, 0,
1591e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    mMaxButtonHeight);
159276d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell            menuWidth = mMenuView.getMeasuredWidth() + getHorizontalMargins(mMenuView);
1593e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mMenuView.getMeasuredHeight() +
1594e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mMenuView));
1595e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mMenuView.getMeasuredState());
1596e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1597e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1598679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        final int contentInsetEnd = getCurrentContentInsetEnd();
15997a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        width += Math.max(contentInsetEnd, menuWidth);
16007a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[marginEndIndex] = Math.max(0, contentInsetEnd - menuWidth);
160176d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
1602e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mExpandedActionView)) {
16037a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += measureChildCollapseMargins(mExpandedActionView, widthMeasureSpec, width,
16047a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, 0, collapsingMargins);
1605e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            height = Math.max(height, mExpandedActionView.getMeasuredHeight() +
1606e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    getVerticalMargins(mExpandedActionView));
1607e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            childState = combineMeasuredStates(childState, mExpandedActionView.getMeasuredState());
1608e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1609e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1610e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mLogoView)) {
16117a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += measureChildCollapseMargins(mLogoView, widthMeasureSpec, width,
16127a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, 0, collapsingMargins);
1613e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            height = Math.max(height, mLogoView.getMeasuredHeight() +
1614e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mLogoView));
1615e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mLogoView.getMeasuredState());
1616e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1617e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1618b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        final int childCount = getChildCount();
1619b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        for (int i = 0; i < childCount; i++) {
1620b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            final View child = getChildAt(i);
1621b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1622b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            if (lp.mViewType != LayoutParams.CUSTOM || !shouldLayout(child)) {
1623b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                // We already got all system views above. Skip them and GONE views.
1624b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                continue;
1625b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            }
1626b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell
1627b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            width += measureChildCollapseMargins(child, widthMeasureSpec, width,
1628b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell                    heightMeasureSpec, 0, collapsingMargins);
1629b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            height = Math.max(height, child.getMeasuredHeight() + getVerticalMargins(child));
1630b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell            childState = combineMeasuredStates(childState, child.getMeasuredState());
1631b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell        }
1632b606a3a312fe6d69d97c66d76803eb2412d8329aAdam Powell
1633e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleWidth = 0;
1634e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleHeight = 0;
1635e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int titleVertMargins = mTitleMarginTop + mTitleMarginBottom;
1636e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int titleHorizMargins = mTitleMarginStart + mTitleMarginEnd;
1637e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mTitleTextView)) {
16387a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            titleWidth = measureChildCollapseMargins(mTitleTextView, widthMeasureSpec,
16397a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    width + titleHorizMargins, heightMeasureSpec, titleVertMargins,
16407a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    collapsingMargins);
1641e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleWidth = mTitleTextView.getMeasuredWidth() + getHorizontalMargins(mTitleTextView);
1642e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight = mTitleTextView.getMeasuredHeight() + getVerticalMargins(mTitleTextView);
1643e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mTitleTextView.getMeasuredState());
1644e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1645e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mSubtitleTextView)) {
16467a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            titleWidth = Math.max(titleWidth, measureChildCollapseMargins(mSubtitleTextView,
16477a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    widthMeasureSpec, width + titleHorizMargins,
16487a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    heightMeasureSpec, titleHeight + titleVertMargins,
16497a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    collapsingMargins));
1650e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight += mSubtitleTextView.getMeasuredHeight() +
1651e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    getVerticalMargins(mSubtitleTextView);
1652e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            childState = combineMeasuredStates(childState, mSubtitleTextView.getMeasuredState());
1653e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1654e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1655e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        width += titleWidth;
1656e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        height = Math.max(height, titleHeight);
1657e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1658e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Measurement already took padding into account for available space for the children,
1659e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // add it in for the final size.
1660e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        width += getPaddingLeft() + getPaddingRight();
1661e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        height += getPaddingTop() + getPaddingBottom();
1662e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1663e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int measuredWidth = resolveSizeAndState(
1664e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                Math.max(width, getSuggestedMinimumWidth()),
1665e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                widthMeasureSpec, childState & MEASURED_STATE_MASK);
1666e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int measuredHeight = resolveSizeAndState(
1667e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                Math.max(height, getSuggestedMinimumHeight()),
1668e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT);
1669360c1d89227570510de7bb11195c986300488464Adam Powell
1670360c1d89227570510de7bb11195c986300488464Adam Powell        setMeasuredDimension(measuredWidth, shouldCollapse() ? 0 : measuredHeight);
1671e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1672e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1673e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
1674e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
1675e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
1676e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int width = getWidth();
1677e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int height = getHeight();
1678e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingLeft = getPaddingLeft();
1679e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingRight = getPaddingRight();
1680e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingTop = getPaddingTop();
1681e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int paddingBottom = getPaddingBottom();
1682e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int left = paddingLeft;
1683e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int right = width - paddingRight;
1684e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
16857a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int[] collapsingMargins = mTempMargins;
16867a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = collapsingMargins[1] = 0;
16877a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1688e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        // Align views within the minimum toolbar height, if set.
1689e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        final int alignmentHeight = getMinimumHeight();
1690e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette
1691e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mNavButtonView)) {
1692e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
1693e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                right = layoutChildRight(mNavButtonView, right, collapsingMargins,
1694e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1695e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
1696e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                left = layoutChildLeft(mNavButtonView, left, collapsingMargins,
1697e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1698e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1699e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1700e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1701e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mCollapseButtonView)) {
1702e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (isRtl) {
1703e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                right = layoutChildRight(mCollapseButtonView, right, collapsingMargins,
1704e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1705e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            } else {
1706e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                left = layoutChildLeft(mCollapseButtonView, left, collapsingMargins,
1707e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1708e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1709e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1710e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1711e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mMenuView)) {
1712e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
1713e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                left = layoutChildLeft(mMenuView, left, collapsingMargins,
1714e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1715e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
1716e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                right = layoutChildRight(mMenuView, right, collapsingMargins,
1717e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1718e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1719e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1720e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1721679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        final int contentInsetLeft = getCurrentContentInsetLeft();
1722679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        final int contentInsetRight = getCurrentContentInsetRight();
1723679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        collapsingMargins[0] = Math.max(0, contentInsetLeft - left);
1724679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        collapsingMargins[1] = Math.max(0, contentInsetRight - (width - paddingRight - right));
1725679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        left = Math.max(left, contentInsetLeft);
1726679be2d6c423d1d2c8f7d94901ddcffcb9510619Adam Powell        right = Math.min(right, width - paddingRight - contentInsetRight);
172776d8f968059d27ef8500cabf7690c18552c22d5aAdam Powell
1728e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (shouldLayout(mExpandedActionView)) {
1729e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (isRtl) {
1730e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                right = layoutChildRight(mExpandedActionView, right, collapsingMargins,
1731e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1732e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            } else {
1733e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                left = layoutChildLeft(mExpandedActionView, left, collapsingMargins,
1734e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1735e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
1736e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
1737e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1738e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (shouldLayout(mLogoView)) {
1739e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
1740e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                right = layoutChildRight(mLogoView, right, collapsingMargins,
1741e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1742e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
1743e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                left = layoutChildLeft(mLogoView, left, collapsingMargins,
1744e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        alignmentHeight);
1745e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1746e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1747e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1748e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean layoutTitle = shouldLayout(mTitleTextView);
1749e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean layoutSubtitle = shouldLayout(mSubtitleTextView);
1750e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int titleHeight = 0;
1751e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutTitle) {
1752e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1753e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            titleHeight += lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
1754e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1755e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutSubtitle) {
1756e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1757b3e411458cd7b96bb308e2de3086efc4da10d64dAdam Powell            titleHeight += lp.topMargin + mSubtitleTextView.getMeasuredHeight() + lp.bottomMargin;
1758e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1759e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1760e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (layoutTitle || layoutSubtitle) {
1761e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            int titleTop;
1762e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View topChild = layoutTitle ? mTitleTextView : mSubtitleTextView;
1763e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View bottomChild = layoutSubtitle ? mSubtitleTextView : mTitleTextView;
1764e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams toplp = (LayoutParams) topChild.getLayoutParams();
1765e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams bottomlp = (LayoutParams) bottomChild.getLayoutParams();
17664c9765a654f7db15091d373f989908da54bfd155Adam Powell            final boolean titleHasWidth = layoutTitle && mTitleTextView.getMeasuredWidth() > 0
17674c9765a654f7db15091d373f989908da54bfd155Adam Powell                    || layoutSubtitle && mSubtitleTextView.getMeasuredWidth() > 0;
1768e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
1769e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) {
1770e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.TOP:
1771e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    titleTop = getPaddingTop() + toplp.topMargin + mTitleMarginTop;
1772e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1773e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                default:
1774e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.CENTER_VERTICAL:
1775e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int space = height - paddingTop - paddingBottom;
1776e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    int spaceAbove = (space - titleHeight) / 2;
1777e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    if (spaceAbove < toplp.topMargin + mTitleMarginTop) {
1778e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        spaceAbove = toplp.topMargin + mTitleMarginTop;
1779e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    } else {
1780e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        final int spaceBelow = height - paddingBottom - titleHeight -
1781e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                                spaceAbove - paddingTop;
1782e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) {
1783e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                            spaceAbove = Math.max(0, spaceAbove -
1784e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                                    (bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow));
1785e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        }
1786e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
1787e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = paddingTop + spaceAbove;
1788e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1789e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                case Gravity.BOTTOM:
1790e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    titleTop = height - paddingBottom - bottomlp.bottomMargin - mTitleMarginBottom -
1791e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            titleHeight;
1792e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    break;
1793e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1794e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            if (isRtl) {
17954c9765a654f7db15091d373f989908da54bfd155Adam Powell                final int rd = (titleHasWidth ? mTitleMarginStart : 0) - collapsingMargins[1];
17967a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                right -= Math.max(0, rd);
17977a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                collapsingMargins[1] = Math.max(0, -rd);
1798e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int titleRight = right;
1799e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int subtitleRight = right;
18007a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1801e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutTitle) {
1802e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1803e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleLeft = titleRight - mTitleTextView.getMeasuredWidth();
1804e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
1805e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
18067a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    titleRight = titleLeft - mTitleMarginEnd;
1807e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = titleBottom + lp.bottomMargin;
1808e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1809e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutSubtitle) {
1810e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1811e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop += lp.topMargin;
1812e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleLeft = subtitleRight - mSubtitleTextView.getMeasuredWidth();
1813e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
1814e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
18157a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    subtitleRight = subtitleRight - mTitleMarginEnd;
1816e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = subtitleBottom + lp.bottomMargin;
1817e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
18184c9765a654f7db15091d373f989908da54bfd155Adam Powell                if (titleHasWidth) {
18194c9765a654f7db15091d373f989908da54bfd155Adam Powell                    right = Math.min(titleRight, subtitleRight);
18204c9765a654f7db15091d373f989908da54bfd155Adam Powell                }
1821e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            } else {
18224c9765a654f7db15091d373f989908da54bfd155Adam Powell                final int ld = (titleHasWidth ? mTitleMarginStart : 0) - collapsingMargins[0];
18237a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                left += Math.max(0, ld);
18247a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                collapsingMargins[0] = Math.max(0, -ld);
1825e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int titleLeft = left;
1826e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int subtitleLeft = left;
18277a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell
1828e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutTitle) {
1829e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
1830e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleRight = titleLeft + mTitleTextView.getMeasuredWidth();
1831e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
1832e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
18337a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    titleLeft = titleRight + mTitleMarginEnd;
1834e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = titleBottom + lp.bottomMargin;
1835e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1836e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (layoutSubtitle) {
1837e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
1838e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop += lp.topMargin;
1839e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleRight = subtitleLeft + mSubtitleTextView.getMeasuredWidth();
1840e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
1841e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
18427a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell                    subtitleLeft = subtitleRight + mTitleMarginEnd;
1843e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    titleTop = subtitleBottom + lp.bottomMargin;
1844e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
18454c9765a654f7db15091d373f989908da54bfd155Adam Powell                if (titleHasWidth) {
18464c9765a654f7db15091d373f989908da54bfd155Adam Powell                    left = Math.max(titleLeft, subtitleLeft);
18474c9765a654f7db15091d373f989908da54bfd155Adam Powell                }
1848e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
1849e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1850e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1851e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Get all remaining children sorted for layout. This is all prepared
1852e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // such that absolute layout direction can be used below.
1853e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1854e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.LEFT);
1855e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int leftViewsCount = mTempViews.size();
1856e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < leftViewsCount; i++) {
1857e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            left = layoutChildLeft(mTempViews.get(i), left, collapsingMargins,
1858e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                    alignmentHeight);
1859e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1860e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1861e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.RIGHT);
1862e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int rightViewsCount = mTempViews.size();
1863e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < rightViewsCount; i++) {
1864e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            right = layoutChildRight(mTempViews.get(i), right, collapsingMargins,
1865e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                    alignmentHeight);
1866e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1867e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1868e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // Centered views try to center with respect to the whole bar, but views pinned
1869e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        // to the left or right can push the mass of centered views to one side or the other.
1870e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        addCustomViewsWithGravity(mTempViews, Gravity.CENTER_HORIZONTAL);
18717a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int centerViewsWidth = getViewListMeasuredWidth(mTempViews, collapsingMargins);
1872e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2;
1873e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int halfCenterViewsWidth = centerViewsWidth / 2;
1874e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int centerLeft = parentCenter - halfCenterViewsWidth;
1875e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int centerRight = centerLeft + centerViewsWidth;
1876e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (centerLeft < left) {
1877e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            centerLeft = left;
1878e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (centerRight > right) {
1879e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            centerLeft -= centerRight - right;
1880e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1881e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1882e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int centerViewsCount = mTempViews.size();
1883e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < centerViewsCount; i++) {
1884e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            centerLeft = layoutChildLeft(mTempViews.get(i), centerLeft, collapsingMargins,
1885e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                    alignmentHeight);
1886e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1887e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette
1888e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        mTempViews.clear();
1889e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1890e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
18917a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell    private int getViewListMeasuredWidth(List<View> views, int[] collapsingMargins) {
18927a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        int collapseLeft = collapsingMargins[0];
18937a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        int collapseRight = collapsingMargins[1];
1894e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int width = 0;
1895e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int count = views.size();
1896e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        for (int i = 0; i < count; i++) {
1897e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final View v = views.get(i);
1898e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            final LayoutParams lp = (LayoutParams) v.getLayoutParams();
18997a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int l = lp.leftMargin - collapseLeft;
19007a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int r = lp.rightMargin - collapseRight;
19017a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int leftMargin = Math.max(0, l);
19027a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            final int rightMargin = Math.max(0, r);
19037a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            collapseLeft = Math.max(0, -l);
19047a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            collapseRight = Math.max(0, -r);
19057a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell            width += leftMargin + v.getMeasuredWidth() + rightMargin;
1906e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1907e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return width;
1908e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1909e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1910e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette    private int layoutChildLeft(View child, int left, int[] collapsingMargins,
1911e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            int alignmentHeight) {
1912e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
19137a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int l = lp.leftMargin - collapsingMargins[0];
19147a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        left += Math.max(0, l);
19157a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[0] = Math.max(0, -l);
1916e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        final int top = getChildTop(child, alignmentHeight);
19177a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int childWidth = child.getMeasuredWidth();
19187a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        child.layout(left, top, left + childWidth, top + child.getMeasuredHeight());
19197a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        left += childWidth + lp.rightMargin;
1920e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return left;
1921e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1922e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1923e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette    private int layoutChildRight(View child, int right, int[] collapsingMargins,
1924e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette            int alignmentHeight) {
1925e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
19267a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        final int r = lp.rightMargin - collapsingMargins[1];
19277a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        right -= Math.max(0, r);
19287a32401c2a62f778da7056cc9aa65bd1489aa153Adam Powell        collapsingMargins[1] = Math.max(0, -r);
1929e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        final int top = getChildTop(child, alignmentHeight);
19307a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        final int childWidth = child.getMeasuredWidth();
19317a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        child.layout(right - childWidth, top, right, top + child.getMeasuredHeight());
19327a8a284d46c740e8b2cd1ccd088c0ba41e020642Adam Powell        right -= childWidth + lp.leftMargin;
1933e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return right;
1934e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1935e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1936e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette    private int getChildTop(View child, int alignmentHeight) {
1937e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1938e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        final int childHeight = child.getMeasuredHeight();
1939e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette        final int alignmentOffset = alignmentHeight > 0 ? (childHeight - alignmentHeight) / 2 : 0;
1940e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (getChildVerticalGravity(lp.gravity)) {
1941e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.TOP:
1942e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                return getPaddingTop() - alignmentOffset;
1943e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1944e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.BOTTOM:
1945e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                return getHeight() - getPaddingBottom() - childHeight
1946e3cb7baeb9563cd78fecbd84f9ab43be62d25013Alan Viverette                        - lp.bottomMargin - alignmentOffset;
1947e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1948e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1949e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_VERTICAL:
1950e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int paddingTop = getPaddingTop();
1951e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int paddingBottom = getPaddingBottom();
1952e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int height = getHeight();
1953e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final int space = height - paddingTop - paddingBottom;
1954e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                int spaceAbove = (space - childHeight) / 2;
1955e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                if (spaceAbove < lp.topMargin) {
1956e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    spaceAbove = lp.topMargin;
1957e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                } else {
1958e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    final int spaceBelow = height - paddingBottom - childHeight -
1959e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                            spaceAbove - paddingTop;
1960e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    if (spaceBelow < lp.bottomMargin) {
1961e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        spaceAbove = Math.max(0, spaceAbove - (lp.bottomMargin - spaceBelow));
1962e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    }
1963e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
1964e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return paddingTop + spaceAbove;
1965e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1966e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1967e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1968e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildVerticalGravity(int gravity) {
1969e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int vgrav = gravity & Gravity.VERTICAL_GRAVITY_MASK;
1970e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (vgrav) {
1971e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.TOP:
1972e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.BOTTOM:
1973e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_VERTICAL:
1974e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return vgrav;
1975e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
1976e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return mGravity & Gravity.VERTICAL_GRAVITY_MASK;
1977e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
1978e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
1979e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1980e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
1981e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Prepare a list of non-SYSTEM child views. If the layout direction is RTL
1982e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * this will be in reverse child order.
1983e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
1984e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param views List to populate. It will be cleared before use.
1985e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @param gravity Horizontal gravity to match against
1986e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
1987e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private void addCustomViewsWithGravity(List<View> views, int gravity) {
1988e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
1989e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int childCount = getChildCount();
1990e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int absGrav = Gravity.getAbsoluteGravity(gravity, getLayoutDirection());
1991e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1992e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        views.clear();
1993e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
1994e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (isRtl) {
1995e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            for (int i = childCount - 1; i >= 0; i--) {
1996e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final View child = getChildAt(i);
1997e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
1998e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
1999e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        getChildHorizontalGravity(lp.gravity) == absGrav) {
2000e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    views.add(child);
2001e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
2002e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
2003e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else {
2004e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            for (int i = 0; i < childCount; i++) {
2005e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final View child = getChildAt(i);
2006e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
2007e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
2008e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                        getChildHorizontalGravity(lp.gravity) == absGrav) {
2009e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                    views.add(child);
2010e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                }
2011e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
2012e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2013e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2014e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2015e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getChildHorizontalGravity(int gravity) {
2016e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int ld = getLayoutDirection();
2017e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int absGrav = Gravity.getAbsoluteGravity(gravity, ld);
2018e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final int hGrav = absGrav & Gravity.HORIZONTAL_GRAVITY_MASK;
2019e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        switch (hGrav) {
2020e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.LEFT:
2021e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.RIGHT:
2022e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            case Gravity.CENTER_HORIZONTAL:
2023e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return hGrav;
2024e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            default:
2025e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return ld == LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT;
2026e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2027e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2028e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2029e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private boolean shouldLayout(View view) {
2030e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return view != null && view.getParent() == this && view.getVisibility() != GONE;
2031e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2032e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2033e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getHorizontalMargins(View v) {
2034e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
2035e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mlp.getMarginStart() + mlp.getMarginEnd();
2036e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2037e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2038e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private int getVerticalMargins(View v) {
2039e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
2040e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return mlp.topMargin + mlp.bottomMargin;
2041e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2042e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2043e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
2044e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public LayoutParams generateLayoutParams(AttributeSet attrs) {
2045e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return new LayoutParams(getContext(), attrs);
2046e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2047e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2048e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
2049e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
2050e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        if (p instanceof LayoutParams) {
2051e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams((LayoutParams) p);
2052e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        } else if (p instanceof ActionBar.LayoutParams) {
2053e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return new LayoutParams((ActionBar.LayoutParams) p);
2054e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else if (p instanceof MarginLayoutParams) {
2055e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams((MarginLayoutParams) p);
2056e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        } else {
2057e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            return new LayoutParams(p);
2058e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2059e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2060e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2061e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
2062e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    protected LayoutParams generateDefaultLayoutParams() {
2063e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
2064e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2065e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2066e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    @Override
2067e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
2068e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return super.checkLayoutParams(p) && p instanceof LayoutParams;
2069e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2070e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2071e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    private static boolean isCustomView(View child) {
2072e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        return ((LayoutParams) child.getLayoutParams()).mViewType == LayoutParams.CUSTOM;
2073e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2074e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2075e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    /** @hide */
2076e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public DecorToolbar getWrapper() {
2077e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        if (mWrapper == null) {
20782aa09a94dd540650d3ebad7363e4fb3aab2ebc95Adam Powell            mWrapper = new ToolbarWidgetWrapper(this, true);
2079e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2080e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        return mWrapper;
2081e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
2082e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
20835f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell    void removeChildrenForExpandedActionView() {
2084e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        final int childCount = getChildCount();
20855f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        // Go backwards since we're removing from the list
20865f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        for (int i = childCount - 1; i >= 0; i--) {
2087e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final View child = getChildAt(i);
2088e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
2089e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (lp.mViewType != LayoutParams.EXPANDED && child != mMenuView) {
20905f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell                removeViewAt(i);
20915f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell                mHiddenViews.add(child);
2092e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
2093e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2094e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
2095e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
20965f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell    void addChildrenForExpandedActionView() {
20975f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        final int count = mHiddenViews.size();
20985f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        // Re-add in reverse order since we removed in reverse order
20995f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        for (int i = count - 1; i >= 0; i--) {
21005f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            addView(mHiddenViews.get(i));
210131c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        }
21025f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        mHiddenViews.clear();
21035f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell    }
21045f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell
21055f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell    private boolean isChildOrHidden(View child) {
21065f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell        return child.getParent() == this || mHiddenViews.contains(child);
210731c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell    }
210831c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
2109e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
2110360c1d89227570510de7bb11195c986300488464Adam Powell     * Force the toolbar to collapse to zero-height during measurement if
2111360c1d89227570510de7bb11195c986300488464Adam Powell     * it could be considered "empty" (no visible elements with nonzero measured size)
2112360c1d89227570510de7bb11195c986300488464Adam Powell     * @hide
2113360c1d89227570510de7bb11195c986300488464Adam Powell     */
2114360c1d89227570510de7bb11195c986300488464Adam Powell    public void setCollapsible(boolean collapsible) {
2115360c1d89227570510de7bb11195c986300488464Adam Powell        mCollapsible = collapsible;
2116360c1d89227570510de7bb11195c986300488464Adam Powell        requestLayout();
2117360c1d89227570510de7bb11195c986300488464Adam Powell    }
2118360c1d89227570510de7bb11195c986300488464Adam Powell
2119360c1d89227570510de7bb11195c986300488464Adam Powell    /**
212004c0d4607897157f01c908475fa0c6d7301cf7e5Adam Powell     * Must be called before the menu is accessed
212104c0d4607897157f01c908475fa0c6d7301cf7e5Adam Powell     * @hide
212204c0d4607897157f01c908475fa0c6d7301cf7e5Adam Powell     */
2123c46125042493e66f7dfdccec24e3b0739600b194Adam Powell    public void setMenuCallbacks(MenuPresenter.Callback pcb, MenuBuilder.Callback mcb) {
2124c46125042493e66f7dfdccec24e3b0739600b194Adam Powell        mActionMenuPresenterCallback = pcb;
2125c46125042493e66f7dfdccec24e3b0739600b194Adam Powell        mMenuBuilderCallback = mcb;
21265190f1737f6eab674262c650b9685aa1f220d706Alan Viverette        if (mMenuView != null) {
21275190f1737f6eab674262c650b9685aa1f220d706Alan Viverette            mMenuView.setMenuCallbacks(pcb, mcb);
21285190f1737f6eab674262c650b9685aa1f220d706Alan Viverette        }
212904c0d4607897157f01c908475fa0c6d7301cf7e5Adam Powell    }
213004c0d4607897157f01c908475fa0c6d7301cf7e5Adam Powell
213104c0d4607897157f01c908475fa0c6d7301cf7e5Adam Powell    /**
21320bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta     * Accessor to enable LayoutLib to get ActionMenuPresenter directly.
21330bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta     */
21340bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta    ActionMenuPresenter getOuterActionMenuPresenter() {
21350bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        return mOuterActionMenuPresenter;
21360bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta    }
21370bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta
21380bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta    Context getPopupContext() {
21390bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta        return mPopupContext;
21400bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta    }
21410bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta
21420bffc736e1f260724764d31f7499cbc09471a535Deepanshu Gupta    /**
2143e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Interface responsible for receiving menu item click events if the items themselves
2144e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * do not have individual item click listeners.
2145e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
2146e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    public interface OnMenuItemClickListener {
2147e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        /**
2148e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * This method will be invoked when a menu item is clicked if the item itself did
2149e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * not already handle the event.
2150e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         *
2151e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * @param item {@link MenuItem} that was clicked
2152e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         * @return <code>true</code> if the event was handled, <code>false</code> otherwise.
2153e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell         */
2154e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public boolean onMenuItemClick(MenuItem item);
2155e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2156e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2157e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    /**
2158e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * Layout information for child views of Toolbars.
2159e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     *
2160d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * <p>Toolbar.LayoutParams extends ActionBar.LayoutParams for compatibility with existing
2161d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * ActionBar API. See {@link android.app.Activity#setActionBar(Toolbar) Activity.setActionBar}
2162d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     * for more info on how to use a Toolbar as your Activity's ActionBar.</p>
2163d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell     *
2164e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     * @attr ref android.R.styleable#Toolbar_LayoutParams_layout_gravity
2165e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell     */
2166e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    public static class LayoutParams extends ActionBar.LayoutParams {
2167e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        static final int CUSTOM = 0;
2168e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        static final int SYSTEM = 1;
2169e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        static final int EXPANDED = 2;
2170e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2171e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        int mViewType = CUSTOM;
2172e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2173e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(@NonNull Context c, AttributeSet attrs) {
2174e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(c, attrs);
2175e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2176e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2177e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int width, int height) {
2178e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(width, height);
2179e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this.gravity = Gravity.CENTER_VERTICAL | Gravity.START;
2180e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2181e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2182e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int width, int height, int gravity) {
2183e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(width, height);
2184e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this.gravity = gravity;
2185e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2186e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2187e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(int gravity) {
2188e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            this(WRAP_CONTENT, MATCH_PARENT, gravity);
2189e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2190e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2191e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(LayoutParams source) {
2192e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
2193e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2194e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mViewType = source.mViewType;
2195e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2196e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2197e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public LayoutParams(ActionBar.LayoutParams source) {
2198e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            super(source);
2199e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2200e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2201e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(MarginLayoutParams source) {
2202e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
2203d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            // ActionBar.LayoutParams doesn't have a MarginLayoutParams constructor.
2204d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            // Fake it here and copy over the relevant data.
2205d76008352eeb246af76ba4a81eaeb4901ca4ef7fAdam Powell            copyMarginsFrom(source);
2206e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2207e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2208e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public LayoutParams(ViewGroup.LayoutParams source) {
2209e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
2210e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2211e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2212e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2213e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    static class SavedState extends BaseSavedState {
221431c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        public int expandedMenuItemId;
221531c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell        public boolean isOverflowOpen;
221631c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell
2217e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public SavedState(Parcel source) {
2218e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(source);
221931c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            expandedMenuItemId = source.readInt();
222031c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            isOverflowOpen = source.readInt() != 0;
2221e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2222e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2223e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public SavedState(Parcelable superState) {
2224e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super(superState);
2225e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2226e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2227e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        @Override
2228e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public void writeToParcel(Parcel out, int flags) {
2229e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            super.writeToParcel(out, flags);
223031c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            out.writeInt(expandedMenuItemId);
223131c91c81798802f62d55f8bccb6d4071471c1aa4Adam Powell            out.writeInt(isOverflowOpen ? 1 : 0);
2232e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        }
2233e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2234e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
2235e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2236e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            @Override
2237e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            public SavedState createFromParcel(Parcel source) {
2238e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return new SavedState(source);
2239e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
2240e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell
2241e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            @Override
2242e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            public SavedState[] newArray(int size) {
2243e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell                return new SavedState[size];
2244e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell            }
2245e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell        };
2246e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell    }
2247e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2248e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    private class ExpandedActionViewMenuPresenter implements MenuPresenter {
2249e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        MenuBuilder mMenu;
2250e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        MenuItemImpl mCurrentExpandedItem;
2251e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2252e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
225328a8468995c71ba3fbba12557d143e7599db38d8Alan Viverette        public void initForMenu(@NonNull Context context, @Nullable MenuBuilder menu) {
2254e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Clear the expanded action view when menus change.
2255e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mMenu != null && mCurrentExpandedItem != null) {
2256e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mMenu.collapseItemActionView(mCurrentExpandedItem);
2257e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
2258e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mMenu = menu;
2259e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2260e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2261e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2262e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public MenuView getMenuView(ViewGroup root) {
2263e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return null;
2264e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2265e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2266e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2267e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void updateMenuView(boolean cleared) {
2268e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Make sure the expanded item we have is still there.
2269e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mCurrentExpandedItem != null) {
2270e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                boolean found = false;
2271e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2272e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (mMenu != null) {
2273e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    final int count = mMenu.size();
2274e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    for (int i = 0; i < count; i++) {
2275e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        final MenuItem item = mMenu.getItem(i);
2276e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        if (item == mCurrentExpandedItem) {
2277e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            found = true;
2278e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                            break;
2279e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                        }
2280e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    }
2281e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
2282e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2283e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                if (!found) {
2284e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    // The item we had expanded disappeared. Collapse.
2285e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                    collapseItemActionView(mMenu, mCurrentExpandedItem);
2286e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                }
2287e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
2288e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2289e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2290e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2291e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void setCallback(Callback cb) {
2292e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2293e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2294e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2295e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
2296e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
2297e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2298e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2299e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2300e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
2301e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2302e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2303e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2304e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean flagActionItems() {
2305e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return false;
2306e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2307e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2308e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2309e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
2310e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            ensureCollapseButtonView();
2311e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mCollapseButtonView.getParent() != Toolbar.this) {
2312e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                addView(mCollapseButtonView);
2313e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
2314e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedActionView = item.getActionView();
2315e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCurrentExpandedItem = item;
2316e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView.getParent() != Toolbar.this) {
2317e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                final LayoutParams lp = generateDefaultLayoutParams();
2318e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
2319e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                lp.mViewType = LayoutParams.EXPANDED;
2320e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                mExpandedActionView.setLayoutParams(lp);
2321e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                addView(mExpandedActionView);
2322e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
2323e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
23245f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            removeChildrenForExpandedActionView();
2325e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            requestLayout();
2326e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.setActionViewExpanded(true);
2327e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2328e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView instanceof CollapsibleActionView) {
2329e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                ((CollapsibleActionView) mExpandedActionView).onActionViewExpanded();
2330e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
2331e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2332e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return true;
2333e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2334e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2335e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2336e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
2337e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // Do this before detaching the actionview from the hierarchy, in case
2338e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            // it needs to dismiss the soft keyboard, etc.
2339e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            if (mExpandedActionView instanceof CollapsibleActionView) {
2340e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell                ((CollapsibleActionView) mExpandedActionView).onActionViewCollapsed();
2341e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            }
2342e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2343e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            removeView(mExpandedActionView);
2344e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            removeView(mCollapseButtonView);
2345e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mExpandedActionView = null;
2346e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
23475f6228454de9bc18847a28e7c21e8fb9bab3d6acAdam Powell            addChildrenForExpandedActionView();
2348e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            mCurrentExpandedItem = null;
2349e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            requestLayout();
2350e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            item.setActionViewExpanded(false);
2351e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2352e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return true;
2353e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2354e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2355e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2356e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public int getId() {
2357e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return 0;
2358e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2359e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2360e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2361e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public Parcelable onSaveInstanceState() {
2362e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell            return null;
2363e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2364e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell
2365e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        @Override
2366e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        public void onRestoreInstanceState(Parcelable state) {
2367e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell        }
2368e021e6ed8931a0a8296af182fc9b0c76b64fb0c4Adam Powell    }
2369e43340c80dc66c45edc793ecd0343774aa34d108Adam Powell}
2370