-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolbar.java
More file actions
2438 lines (2167 loc) · 92.1 KB
/
Copy pathToolbar.java
File metadata and controls
2438 lines (2167 loc) · 92.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.appcompat.widget;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.MenuRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.R;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.view.CollapsibleActionView;
import androidx.appcompat.view.SupportMenuInflater;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.view.menu.MenuItemImpl;
import androidx.appcompat.view.menu.MenuPresenter;
import androidx.appcompat.view.menu.MenuView;
import androidx.appcompat.view.menu.SubMenuBuilder;
import androidx.core.view.GravityCompat;
import androidx.core.view.MarginLayoutParamsCompat;
import androidx.core.view.ViewCompat;
import androidx.customview.view.AbsSavedState;
import java.util.ArrayList;
import java.util.List;
/**
* A standard toolbar for use within application content.
*
* <p>A Toolbar is a generalization of {@link ActionBar action bars} for use
* within application layouts. While an action bar is traditionally part of an
* {@link android.app.Activity Activity's} opaque window decor controlled by the framework,
* a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy.
* An application may choose to designate a Toolbar as the action bar for an Activity
* using the {@link androidx.appcompat.app.AppCompatActivity#setSupportActionBar(Toolbar)
* setSupportActionBar()} method.</p>
*
* <p>Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar
* may contain a combination of the following optional elements:
*
* <ul>
* <li><em>A navigation button.</em> This may be an Up arrow, navigation menu toggle, close,
* collapse, done or another glyph of the app's choosing. This button should always be used
* to access other navigational destinations within the container of the Toolbar and
* its signified content or otherwise leave the current context signified by the Toolbar.
* The navigation button is vertically aligned within the Toolbar's minimum height,
* if set.</li>
* <li><em>A branded logo image.</em> This may extend to the height of the bar and can be
* arbitrarily wide.</li>
* <li><em>A title and subtitle.</em> The title should be a signpost for the Toolbar's current
* position in the navigation hierarchy and the content contained there. The subtitle,
* if present should indicate any extended information about the current content.
* If an app uses a logo image it should strongly consider omitting a title and subtitle.</li>
* <li><em>One or more custom views.</em> The application may add arbitrary child views
* to the Toolbar. They will appear at this position within the layout. If a child view's
* {@link LayoutParams} indicates a {@link Gravity} value of
* {@link Gravity#CENTER_HORIZONTAL CENTER_HORIZONTAL} the view will attempt to center
* within the available space remaining in the Toolbar after all other elements have been
* measured.</li>
* <li><em>An {@link ActionMenuView action menu}.</em> The menu of actions will pin to the
* end of the Toolbar offering a few
* <a href="http://developer.android.com/design/patterns/actionbar.html#ActionButtons">
* frequent, important or typical</a> actions along with an optional overflow menu for
* additional actions. Action buttons are vertically aligned within the Toolbar's
* minimum height, if set.</li>
* </ul>
* </p>
*
* <p>In modern Android UIs developers should lean more on a visually distinct color scheme for
* toolbars than on their application icon. The use of application icon plus title as a standard
* layout is discouraged on API 21 devices and newer.</p>
*
* @attr ref androidx.appcompat.R.styleable#Toolbar_buttonGravity
* @attr ref androidx.appcompat.R.styleable#Toolbar_collapseContentDescription
* @attr ref androidx.appcompat.R.styleable#Toolbar_collapseIcon
* @attr ref androidx.appcompat.R.styleable#Toolbar_contentInsetEnd
* @attr ref androidx.appcompat.R.styleable#Toolbar_contentInsetLeft
* @attr ref androidx.appcompat.R.styleable#Toolbar_contentInsetRight
* @attr ref androidx.appcompat.R.styleable#Toolbar_contentInsetStart
* @attr ref androidx.appcompat.R.styleable#Toolbar_contentInsetStartWithNavigation
* @attr ref androidx.appcompat.R.styleable#Toolbar_contentInsetEndWithActions
* @attr ref androidx.appcompat.R.styleable#Toolbar_android_gravity
* @attr ref androidx.appcompat.R.styleable#Toolbar_logo
* @attr ref androidx.appcompat.R.styleable#Toolbar_logoDescription
* @attr ref androidx.appcompat.R.styleable#Toolbar_maxButtonHeight
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationContentDescription
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationIcon
* @attr ref androidx.appcompat.R.styleable#Toolbar_popupTheme
* @attr ref androidx.appcompat.R.styleable#Toolbar_subtitle
* @attr ref androidx.appcompat.R.styleable#Toolbar_subtitleTextAppearance
* @attr ref androidx.appcompat.R.styleable#Toolbar_subtitleTextColor
* @attr ref androidx.appcompat.R.styleable#Toolbar_title
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMargin
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginBottom
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginEnd
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginStart
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginTop
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleTextAppearance
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleTextColor
*/
public class Toolbar extends ViewGroup {
private static final String TAG = "Toolbar";
private ActionMenuView mMenuView;
private TextView mTitleTextView;
private TextView mSubtitleTextView;
private ImageButton mNavButtonView;
private ImageView mLogoView;
private Drawable mCollapseIcon;
private CharSequence mCollapseDescription;
ImageButton mCollapseButtonView;
View mExpandedActionView;
/** Context against which to inflate popup menus. */
private Context mPopupContext;
/** Theme resource against which to inflate popup menus. */
private int mPopupTheme;
private int mTitleTextAppearance;
private int mSubtitleTextAppearance;
int mButtonGravity;
private int mMaxButtonHeight;
private int mTitleMarginStart;
private int mTitleMarginEnd;
private int mTitleMarginTop;
private int mTitleMarginBottom;
private RtlSpacingHelper mContentInsets;
private int mContentInsetStartWithNavigation;
private int mContentInsetEndWithActions;
private int mGravity = GravityCompat.START | Gravity.CENTER_VERTICAL;
private CharSequence mTitleText;
private CharSequence mSubtitleText;
private int mTitleTextColor;
private int mSubtitleTextColor;
private boolean mEatingTouch;
private boolean mEatingHover;
// Clear me after use.
private final ArrayList<View> mTempViews = new ArrayList<View>();
// Used to hold views that will be removed while we have an expanded action view.
private final ArrayList<View> mHiddenViews = new ArrayList<>();
private final int[] mTempMargins = new int[2];
OnMenuItemClickListener mOnMenuItemClickListener;
private final ActionMenuView.OnMenuItemClickListener mMenuViewItemClickListener =
new ActionMenuView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (mOnMenuItemClickListener != null) {
return mOnMenuItemClickListener.onMenuItemClick(item);
}
return false;
}
};
private ToolbarWidgetWrapper mWrapper;
private ActionMenuPresenter mOuterActionMenuPresenter;
private ExpandedActionViewMenuPresenter mExpandedMenuPresenter;
private MenuPresenter.Callback mActionMenuPresenterCallback;
private MenuBuilder.Callback mMenuBuilderCallback;
private boolean mCollapsible;
private final Runnable mShowOverflowMenuRunnable = new Runnable() {
@Override public void run() {
showOverflowMenu();
}
};
public Toolbar(Context context) {
this(context, null);
}
public Toolbar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.toolbarStyle);
}
public Toolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// Need to use getContext() here so that we use the themed context
final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
R.styleable.Toolbar, defStyleAttr, 0);
mTitleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
mSubtitleTextAppearance = a.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, 0);
mGravity = a.getInteger(R.styleable.Toolbar_android_gravity, mGravity);
mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);
// First read the correct attribute
int titleMargin = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMargin, 0);
if (a.hasValue(R.styleable.Toolbar_titleMargins)) {
// Now read the deprecated attribute, if it has a value
titleMargin = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMargins, titleMargin);
}
mTitleMarginStart = mTitleMarginEnd = mTitleMarginTop = mTitleMarginBottom = titleMargin;
final int marginStart = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginStart, -1);
if (marginStart >= 0) {
mTitleMarginStart = marginStart;
}
final int marginEnd = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginEnd, -1);
if (marginEnd >= 0) {
mTitleMarginEnd = marginEnd;
}
final int marginTop = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginTop, -1);
if (marginTop >= 0) {
mTitleMarginTop = marginTop;
}
final int marginBottom = a.getDimensionPixelOffset(R.styleable.Toolbar_titleMarginBottom,
-1);
if (marginBottom >= 0) {
mTitleMarginBottom = marginBottom;
}
mMaxButtonHeight = a.getDimensionPixelSize(R.styleable.Toolbar_maxButtonHeight, -1);
final int contentInsetStart =
a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetStart,
RtlSpacingHelper.UNDEFINED);
final int contentInsetEnd =
a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetEnd,
RtlSpacingHelper.UNDEFINED);
final int contentInsetLeft =
a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetLeft, 0);
final int contentInsetRight =
a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0);
ensureContentInsets();
mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
if (contentInsetStart != RtlSpacingHelper.UNDEFINED ||
contentInsetEnd != RtlSpacingHelper.UNDEFINED) {
mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
}
mContentInsetStartWithNavigation = a.getDimensionPixelOffset(
R.styleable.Toolbar_contentInsetStartWithNavigation, RtlSpacingHelper.UNDEFINED);
mContentInsetEndWithActions = a.getDimensionPixelOffset(
R.styleable.Toolbar_contentInsetEndWithActions, RtlSpacingHelper.UNDEFINED);
mCollapseIcon = a.getDrawable(R.styleable.Toolbar_collapseIcon);
mCollapseDescription = a.getText(R.styleable.Toolbar_collapseContentDescription);
final CharSequence title = a.getText(R.styleable.Toolbar_title);
if (!TextUtils.isEmpty(title)) {
setTitle(title);
}
final CharSequence subtitle = a.getText(R.styleable.Toolbar_subtitle);
if (!TextUtils.isEmpty(subtitle)) {
setSubtitle(subtitle);
}
// Set the default context, since setPopupTheme() may be a no-op.
mPopupContext = getContext();
setPopupTheme(a.getResourceId(R.styleable.Toolbar_popupTheme, 0));
final Drawable navIcon = a.getDrawable(R.styleable.Toolbar_navigationIcon);
if (navIcon != null) {
setNavigationIcon(navIcon);
}
final CharSequence navDesc = a.getText(R.styleable.Toolbar_navigationContentDescription);
if (!TextUtils.isEmpty(navDesc)) {
setNavigationContentDescription(navDesc);
}
final Drawable logo = a.getDrawable(R.styleable.Toolbar_logo);
if (logo != null) {
setLogo(logo);
}
final CharSequence logoDesc = a.getText(R.styleable.Toolbar_logoDescription);
if (!TextUtils.isEmpty(logoDesc)) {
setLogoDescription(logoDesc);
}
if (a.hasValue(R.styleable.Toolbar_titleTextColor)) {
setTitleTextColor(a.getColor(R.styleable.Toolbar_titleTextColor, 0xffffffff));
}
if (a.hasValue(R.styleable.Toolbar_subtitleTextColor)) {
setSubtitleTextColor(a.getColor(R.styleable.Toolbar_subtitleTextColor, 0xffffffff));
}
a.recycle();
}
/**
* Specifies the theme to use when inflating popup menus. By default, uses
* the same theme as the toolbar itself.
*
* @param resId theme used to inflate popup menus
* @see #getPopupTheme()
*/
public void setPopupTheme(@StyleRes int resId) {
if (mPopupTheme != resId) {
mPopupTheme = resId;
if (resId == 0) {
mPopupContext = getContext();
} else {
mPopupContext = new ContextThemeWrapper(getContext(), resId);
}
}
}
/**
* @return resource identifier of the theme used to inflate popup menus, or
* 0 if menus are inflated against the toolbar theme
* @see #setPopupTheme(int)
*/
public int getPopupTheme() {
return mPopupTheme;
}
/**
* Sets the title margin.
*
* @param start the starting title margin in pixels
* @param top the top title margin in pixels
* @param end the ending title margin in pixels
* @param bottom the bottom title margin in pixels
* @see #getTitleMarginStart()
* @see #getTitleMarginTop()
* @see #getTitleMarginEnd()
* @see #getTitleMarginBottom()
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMargin
*/
public void setTitleMargin(int start, int top, int end, int bottom) {
mTitleMarginStart = start;
mTitleMarginTop = top;
mTitleMarginEnd = end;
mTitleMarginBottom = bottom;
requestLayout();
}
/**
* @return the starting title margin in pixels
* @see #setTitleMarginStart(int)
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginStart
*/
public int getTitleMarginStart() {
return mTitleMarginStart;
}
/**
* Sets the starting title margin in pixels.
*
* @param margin the starting title margin in pixels
* @see #getTitleMarginStart()
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginStart
*/
public void setTitleMarginStart(int margin) {
mTitleMarginStart = margin;
requestLayout();
}
/**
* @return the top title margin in pixels
* @see #setTitleMarginTop(int)
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginTop
*/
public int getTitleMarginTop() {
return mTitleMarginTop;
}
/**
* Sets the top title margin in pixels.
*
* @param margin the top title margin in pixels
* @see #getTitleMarginTop()
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginTop
*/
public void setTitleMarginTop(int margin) {
mTitleMarginTop = margin;
requestLayout();
}
/**
* @return the ending title margin in pixels
* @see #setTitleMarginEnd(int)
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginEnd
*/
public int getTitleMarginEnd() {
return mTitleMarginEnd;
}
/**
* Sets the ending title margin in pixels.
*
* @param margin the ending title margin in pixels
* @see #getTitleMarginEnd()
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginEnd
*/
public void setTitleMarginEnd(int margin) {
mTitleMarginEnd = margin;
requestLayout();
}
/**
* @return the bottom title margin in pixels
* @see #setTitleMarginBottom(int)
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginBottom
*/
public int getTitleMarginBottom() {
return mTitleMarginBottom;
}
/**
* Sets the bottom title margin in pixels.
*
* @param margin the bottom title margin in pixels
* @see #getTitleMarginBottom()
* @attr ref androidx.appcompat.R.styleable#Toolbar_titleMarginBottom
*/
public void setTitleMarginBottom(int margin) {
mTitleMarginBottom = margin;
requestLayout();
}
@Override
public void onRtlPropertiesChanged(int layoutDirection) {
if (Build.VERSION.SDK_INT >= 17) {
super.onRtlPropertiesChanged(layoutDirection);
}
ensureContentInsets();
mContentInsets.setDirection(layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL);
}
/**
* Set a logo drawable from a resource id.
*
* <p>This drawable should generally take the place of title text. The logo cannot be
* clicked. Apps using a logo should also supply a description using
* {@link #setLogoDescription(int)}.</p>
*
* @param resId ID of a drawable resource
*/
public void setLogo(@DrawableRes int resId) {
setLogo(AppCompatResources.getDrawable(getContext(), resId));
}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
public boolean canShowOverflowMenu() {
return getVisibility() == VISIBLE && mMenuView != null && mMenuView.isOverflowReserved();
}
/**
* Check whether the overflow menu is currently showing. This may not reflect
* a pending show operation in progress.
*
* @return true if the overflow menu is currently showing
*/
public boolean isOverflowMenuShowing() {
return mMenuView != null && mMenuView.isOverflowMenuShowing();
}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
public boolean isOverflowMenuShowPending() {
return mMenuView != null && mMenuView.isOverflowMenuShowPending();
}
/**
* Show the overflow items from the associated menu.
*
* @return true if the menu was able to be shown, false otherwise
*/
public boolean showOverflowMenu() {
return mMenuView != null && mMenuView.showOverflowMenu();
}
/**
* Hide the overflow items from the associated menu.
*
* @return true if the menu was able to be hidden, false otherwise
*/
public boolean hideOverflowMenu() {
return mMenuView != null && mMenuView.hideOverflowMenu();
}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
public void setMenu(MenuBuilder menu, ActionMenuPresenter outerPresenter) {
if (menu == null && mMenuView == null) {
return;
}
ensureMenuView();
final MenuBuilder oldMenu = mMenuView.peekMenu();
if (oldMenu == menu) {
return;
}
if (oldMenu != null) {
oldMenu.removeMenuPresenter(mOuterActionMenuPresenter);
oldMenu.removeMenuPresenter(mExpandedMenuPresenter);
}
if (mExpandedMenuPresenter == null) {
mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
}
outerPresenter.setExpandedActionViewsExclusive(true);
if (menu != null) {
menu.addMenuPresenter(outerPresenter, mPopupContext);
menu.addMenuPresenter(mExpandedMenuPresenter, mPopupContext);
} else {
outerPresenter.initForMenu(mPopupContext, null);
mExpandedMenuPresenter.initForMenu(mPopupContext, null);
outerPresenter.updateMenuView(true);
mExpandedMenuPresenter.updateMenuView(true);
}
mMenuView.setPopupTheme(mPopupTheme);
mMenuView.setPresenter(outerPresenter);
mOuterActionMenuPresenter = outerPresenter;
}
/**
* Dismiss all currently showing popup menus, including overflow or submenus.
*/
public void dismissPopupMenus() {
if (mMenuView != null) {
mMenuView.dismissPopupMenus();
}
}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
public boolean isTitleTruncated() {
if (mTitleTextView == null) {
return false;
}
final Layout titleLayout = mTitleTextView.getLayout();
if (titleLayout == null) {
return false;
}
final int lineCount = titleLayout.getLineCount();
for (int i = 0; i < lineCount; i++) {
if (titleLayout.getEllipsisCount(i) > 0) {
return true;
}
}
return false;
}
/**
* Set a logo drawable.
*
* <p>This drawable should generally take the place of title text. The logo cannot be
* clicked. Apps using a logo should also supply a description using
* {@link #setLogoDescription(int)}.</p>
*
* @param drawable Drawable to use as a logo
*/
public void setLogo(Drawable drawable) {
if (drawable != null) {
ensureLogoView();
if (!isChildOrHidden(mLogoView)) {
addSystemView(mLogoView, true);
}
} else if (mLogoView != null && isChildOrHidden(mLogoView)) {
removeView(mLogoView);
mHiddenViews.remove(mLogoView);
}
if (mLogoView != null) {
mLogoView.setImageDrawable(drawable);
}
}
/**
* Return the current logo drawable.
*
* @return The current logo drawable
* @see #setLogo(int)
* @see #setLogo(android.graphics.drawable.Drawable)
*/
public Drawable getLogo() {
return mLogoView != null ? mLogoView.getDrawable() : null;
}
/**
* Set a description of the toolbar's logo.
*
* <p>This description will be used for accessibility or other similar descriptions
* of the UI.</p>
*
* @param resId String resource id
*/
public void setLogoDescription(@StringRes int resId) {
setLogoDescription(getContext().getText(resId));
}
/**
* Set a description of the toolbar's logo.
*
* <p>This description will be used for accessibility or other similar descriptions
* of the UI.</p>
*
* @param description Description to set
*/
public void setLogoDescription(CharSequence description) {
if (!TextUtils.isEmpty(description)) {
ensureLogoView();
}
if (mLogoView != null) {
mLogoView.setContentDescription(description);
}
}
/**
* Return the description of the toolbar's logo.
*
* @return A description of the logo
*/
public CharSequence getLogoDescription() {
return mLogoView != null ? mLogoView.getContentDescription() : null;
}
private void ensureLogoView() {
if (mLogoView == null) {
mLogoView = new AppCompatImageView(getContext());
}
}
/**
* Check whether this Toolbar is currently hosting an expanded action view.
*
* <p>An action view may be expanded either directly from the
* {@link android.view.MenuItem MenuItem} it belongs to or by user action. If the Toolbar
* has an expanded action view it can be collapsed using the {@link #collapseActionView()}
* method.</p>
*
* @return true if the Toolbar has an expanded action view
*/
public boolean hasExpandedActionView() {
return mExpandedMenuPresenter != null &&
mExpandedMenuPresenter.mCurrentExpandedItem != null;
}
/**
* Collapse a currently expanded action view. If this Toolbar does not have an
* expanded action view this method has no effect.
*
* <p>An action view may be expanded either directly from the
* {@link android.view.MenuItem MenuItem} it belongs to or by user action.</p>
*
* @see #hasExpandedActionView()
*/
public void collapseActionView() {
final MenuItemImpl item = mExpandedMenuPresenter == null ? null :
mExpandedMenuPresenter.mCurrentExpandedItem;
if (item != null) {
item.collapseActionView();
}
}
/**
* Returns the title of this toolbar.
*
* @return The current title.
*/
public CharSequence getTitle() {
return mTitleText;
}
/**
* Set the title of this toolbar.
*
* <p>A title should be used as the anchor for a section of content. It should
* describe or name the content being viewed.</p>
*
* @param resId Resource ID of a string to set as the title
*/
public void setTitle(@StringRes int resId) {
setTitle(getContext().getText(resId));
}
/**
* Set the title of this toolbar.
*
* <p>A title should be used as the anchor for a section of content. It should
* describe or name the content being viewed.</p>
*
* @param title Title to set
*/
public void setTitle(CharSequence title) {
if (!TextUtils.isEmpty(title)) {
if (mTitleTextView == null) {
final Context context = getContext();
mTitleTextView = new AppCompatTextView(context);
mTitleTextView.setSingleLine();
mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
if (mTitleTextAppearance != 0) {
mTitleTextView.setTextAppearance(context, mTitleTextAppearance);
}
if (mTitleTextColor != 0) {
mTitleTextView.setTextColor(mTitleTextColor);
}
}
if (!isChildOrHidden(mTitleTextView)) {
addSystemView(mTitleTextView, true);
}
} else if (mTitleTextView != null && isChildOrHidden(mTitleTextView)) {
removeView(mTitleTextView);
mHiddenViews.remove(mTitleTextView);
}
if (mTitleTextView != null) {
mTitleTextView.setText(title);
}
mTitleText = title;
}
/**
* Return the subtitle of this toolbar.
*
* @return The current subtitle
*/
public CharSequence getSubtitle() {
return mSubtitleText;
}
/**
* Set the subtitle of this toolbar.
*
* <p>Subtitles should express extended information about the current content.</p>
*
* @param resId String resource ID
*/
public void setSubtitle(@StringRes int resId) {
setSubtitle(getContext().getText(resId));
}
/**
* Set the subtitle of this toolbar.
*
* <p>Subtitles should express extended information about the current content.</p>
*
* @param subtitle Subtitle to set
*/
public void setSubtitle(CharSequence subtitle) {
if (!TextUtils.isEmpty(subtitle)) {
if (mSubtitleTextView == null) {
final Context context = getContext();
mSubtitleTextView = new AppCompatTextView(context);
mSubtitleTextView.setSingleLine();
mSubtitleTextView.setEllipsize(TextUtils.TruncateAt.END);
if (mSubtitleTextAppearance != 0) {
mSubtitleTextView.setTextAppearance(context, mSubtitleTextAppearance);
}
if (mSubtitleTextColor != 0) {
mSubtitleTextView.setTextColor(mSubtitleTextColor);
}
}
if (!isChildOrHidden(mSubtitleTextView)) {
addSystemView(mSubtitleTextView, true);
}
} else if (mSubtitleTextView != null && isChildOrHidden(mSubtitleTextView)) {
removeView(mSubtitleTextView);
mHiddenViews.remove(mSubtitleTextView);
}
if (mSubtitleTextView != null) {
mSubtitleTextView.setText(subtitle);
}
mSubtitleText = subtitle;
}
/**
* Sets the text color, size, style, hint color, and highlight color
* from the specified TextAppearance resource.
*/
public void setTitleTextAppearance(Context context, @StyleRes int resId) {
mTitleTextAppearance = resId;
if (mTitleTextView != null) {
mTitleTextView.setTextAppearance(context, resId);
}
}
/**
* Sets the text color, size, style, hint color, and highlight color
* from the specified TextAppearance resource.
*/
public void setSubtitleTextAppearance(Context context, @StyleRes int resId) {
mSubtitleTextAppearance = resId;
if (mSubtitleTextView != null) {
mSubtitleTextView.setTextAppearance(context, resId);
}
}
/**
* Sets the text color of the title, if present.
*
* @param color The new text color in 0xAARRGGBB format
*/
public void setTitleTextColor(@ColorInt int color) {
mTitleTextColor = color;
if (mTitleTextView != null) {
mTitleTextView.setTextColor(color);
}
}
/**
* Sets the text color of the subtitle, if present.
*
* @param color The new text color in 0xAARRGGBB format
*/
public void setSubtitleTextColor(@ColorInt int color) {
mSubtitleTextColor = color;
if (mSubtitleTextView != null) {
mSubtitleTextView.setTextColor(color);
}
}
/**
* Retrieve the currently configured content description for the navigation button view.
* This will be used to describe the navigation action to users through mechanisms such
* as screen readers or tooltips.
*
* @return The navigation button's content description
*
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationContentDescription
*/
@Nullable
public CharSequence getNavigationContentDescription() {
return mNavButtonView != null ? mNavButtonView.getContentDescription() : null;
}
/**
* Set a content description for the navigation button if one is present. The content
* description will be read via screen readers or other accessibility systems to explain
* the action of the navigation button.
*
* @param resId Resource ID of a content description string to set, or 0 to
* clear the description
*
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationContentDescription
*/
public void setNavigationContentDescription(@StringRes int resId) {
setNavigationContentDescription(resId != 0 ? getContext().getText(resId) : null);
}
/**
* Set a content description for the navigation button if one is present. The content
* description will be read via screen readers or other accessibility systems to explain
* the action of the navigation button.
*
* @param description Content description to set, or <code>null</code> to
* clear the content description
*
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationContentDescription
*/
public void setNavigationContentDescription(@Nullable CharSequence description) {
if (!TextUtils.isEmpty(description)) {
ensureNavButtonView();
}
if (mNavButtonView != null) {
mNavButtonView.setContentDescription(description);
}
}
/**
* Set the icon to use for the toolbar's navigation button.
*
* <p>The navigation button appears at the start of the toolbar if present. Setting an icon
* will make the navigation button visible.</p>
*
* <p>If you use a navigation icon you should also set a description for its action using
* {@link #setNavigationContentDescription(int)}. This is used for accessibility and
* tooltips.</p>
*
* @param resId Resource ID of a drawable to set
*
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationIcon
*/
public void setNavigationIcon(@DrawableRes int resId) {
setNavigationIcon(AppCompatResources.getDrawable(getContext(), resId));
}
/**
* Set the icon to use for the toolbar's navigation button.
*
* <p>The navigation button appears at the start of the toolbar if present. Setting an icon
* will make the navigation button visible.</p>
*
* <p>If you use a navigation icon you should also set a description for its action using
* {@link #setNavigationContentDescription(int)}. This is used for accessibility and
* tooltips.</p>
*
* @param icon Drawable to set, may be null to clear the icon
*
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationIcon
*/
public void setNavigationIcon(@Nullable Drawable icon) {
if (icon != null) {
ensureNavButtonView();
if (!isChildOrHidden(mNavButtonView)) {
addSystemView(mNavButtonView, true);
}
} else if (mNavButtonView != null && isChildOrHidden(mNavButtonView)) {
removeView(mNavButtonView);
mHiddenViews.remove(mNavButtonView);
}
if (mNavButtonView != null) {
mNavButtonView.setImageDrawable(icon);
}
}
/**
* Return the current drawable used as the navigation icon.
*
* @return The navigation icon drawable
*
* @attr ref androidx.appcompat.R.styleable#Toolbar_navigationIcon
*/
@Nullable
public Drawable getNavigationIcon() {
return mNavButtonView != null ? mNavButtonView.getDrawable() : null;
}
/**
* Set a listener to respond to navigation events.
*
* <p>This listener will be called whenever the user clicks the navigation button
* at the start of the toolbar. An icon must be set for the navigation button to appear.</p>
*
* @param listener Listener to set
* @see #setNavigationIcon(android.graphics.drawable.Drawable)
*/
public void setNavigationOnClickListener(OnClickListener listener) {
ensureNavButtonView();
mNavButtonView.setOnClickListener(listener);
}
/**
* Return the Menu shown in the toolbar.
*