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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
|
/*
* 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 android.widget;
import android.annotation.NonNull;
import android.app.ActionBar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.CollapsibleActionView;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.Window;
import com.android.internal.R;
import com.android.internal.view.menu.MenuBuilder;
import com.android.internal.view.menu.MenuItemImpl;
import com.android.internal.view.menu.MenuPresenter;
import com.android.internal.view.menu.MenuView;
import com.android.internal.view.menu.SubMenuBuilder;
import com.android.internal.widget.DecorToolbar;
import com.android.internal.widget.ToolbarWidgetWrapper;
import java.util.ArrayList;
import java.util.List;
/**
* A standard toolbar for use within application content.
*
* <p>A Toolbar is a generalization of {@link android.app.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 android.app.Activity#setActionBar(Toolbar) setActionBar()} 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.</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.</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>
*/
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 ImageButton mCollapseButtonView;
View mExpandedActionView;
private int mTitleTextAppearance;
private int mSubtitleTextAppearance;
private int mNavButtonStyle;
private int mButtonGravity;
private int mMaxButtonHeight;
private int mTitleMarginStart;
private int mTitleMarginEnd;
private int mTitleMarginTop;
private int mTitleMarginBottom;
private final RtlSpacingHelper mContentInsets = new RtlSpacingHelper();
private int mGravity = Gravity.START | Gravity.CENTER_VERTICAL;
private CharSequence mTitleText;
private CharSequence mSubtitleText;
// Clear me after use.
private final ArrayList<View> mTempViews = new ArrayList<View>();
private final int[] mTempMargins = new int[2];
private 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;
public Toolbar(Context context) {
this(context, null);
}
public Toolbar(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.toolbarStyle);
}
public Toolbar(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public Toolbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Toolbar,
defStyleAttr, defStyleRes);
mTitleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, 0);
mSubtitleTextAppearance = a.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, 0);
mNavButtonStyle = a.getResourceId(R.styleable.Toolbar_navigationButtonStyle, 0);
mGravity = a.getInteger(R.styleable.Toolbar_gravity, mGravity);
mButtonGravity = a.getInteger(R.styleable.Toolbar_buttonGravity, Gravity.TOP);
mTitleMarginStart = mTitleMarginEnd = mTitleMarginTop = mTitleMarginBottom =
a.getDimensionPixelOffset(R.styleable.Toolbar_titleMargins, 0);
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);
mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
if (contentInsetStart != RtlSpacingHelper.UNDEFINED ||
contentInsetEnd != RtlSpacingHelper.UNDEFINED) {
mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
}
mCollapseIcon = a.getDrawable(R.styleable.Toolbar_collapseIcon);
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(title);
}
a.recycle();
}
@Override
public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection);
mContentInsets.setDirection(layoutDirection == 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(int resId) {
setLogo(getContext().getDrawable(resId));
}
/** @hide */
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 */
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 */
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);
}
final Context context = getContext();
if (mExpandedMenuPresenter == null) {
mExpandedMenuPresenter = new ExpandedActionViewMenuPresenter();
}
outerPresenter.setExpandedActionViewsExclusive(true);
if (menu != null) {
menu.addMenuPresenter(outerPresenter);
menu.addMenuPresenter(mExpandedMenuPresenter);
} else {
outerPresenter.initForMenu(context, null);
mExpandedMenuPresenter.initForMenu(context, null);
outerPresenter.updateMenuView(true);
mExpandedMenuPresenter.updateMenuView(true);
}
mMenuView.setPresenter(outerPresenter);
mOuterActionMenuPresenter = outerPresenter;
}
/**
* Dismiss all currently showing popup menus, including overflow or submenus.
*/
public void dismissPopupMenus() {
if (mMenuView != null) {
mMenuView.dismissPopupMenus();
}
}
/** @hide */
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 (mLogoView.getParent() == null) {
addSystemView(mLogoView);
}
} else if (mLogoView != null && mLogoView.getParent() != null) {
removeView(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(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 ImageView(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(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 TextView(context);
mTitleTextView.setSingleLine();
mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
mTitleTextView.setTextAppearance(context, mTitleTextAppearance);
}
if (mTitleTextView.getParent() == null) {
addSystemView(mTitleTextView);
}
} else if (mTitleTextView != null && mTitleTextView.getParent() != null) {
removeView(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(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 TextView(context);
mSubtitleTextView.setSingleLine();
mSubtitleTextView.setEllipsize(TextUtils.TruncateAt.END);
mSubtitleTextView.setTextAppearance(context, mSubtitleTextAppearance);
}
if (mSubtitleTextView.getParent() == null) {
addSystemView(mSubtitleTextView);
}
} else if (mSubtitleTextView != null && mSubtitleTextView.getParent() != null) {
removeView(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, 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, int resId) {
mSubtitleTextAppearance = resId;
if (mSubtitleTextView != null) {
mSubtitleTextView.setTextAppearance(context, 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 #setNavigationDescription(int)}. This is used for accessibility and tooltips.</p>
*
* @param resId Resource ID of a drawable to set
*/
public void setNavigationIcon(int resId) {
setNavigationIcon(getContext().getDrawable(resId));
}
/**
* 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
*/
public void setNavigationContentDescription(CharSequence description) {
ensureNavButtonView();
mNavButtonView.setContentDescription(description);
}
/**
* 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
*/
public void setNavigationContentDescription(int resId) {
ensureNavButtonView();
mNavButtonView.setContentDescription(resId != 0 ? getContext().getText(resId) : null);
}
/**
* 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 #setNavigationDescription(int)}. This is used for accessibility and tooltips.</p>
*
* @param icon Drawable to set
*/
public void setNavigationIcon(Drawable icon) {
if (icon != null) {
ensureNavButtonView();
if (mNavButtonView.getParent() == null) {
addSystemView(mNavButtonView);
}
} else if (mNavButtonView != null && mNavButtonView.getParent() != null) {
removeView(mNavButtonView);
}
if (mNavButtonView != null) {
mNavButtonView.setImageDrawable(icon);
}
}
/**
* Return the current drawable used as the navigation icon.
*
* @return The navigation icon drawable
*/
public Drawable getNavigationIcon() {
return mNavButtonView != null ? mNavButtonView.getDrawable() : null;
}
/**
* Set a description for the navigation button.
*
* <p>This description string is used for accessibility, tooltips and other facilities
* to improve discoverability.</p>
*
* @param resId Resource ID of a string to set
*/
public void setNavigationDescription(int resId) {
setNavigationDescription(getContext().getText(resId));
}
/**
* Set a description for the navigation button.
*
* <p>This description string is used for accessibility, tooltips and other facilities
* to improve discoverability.</p>
*
* @param description String to set as the description
*/
public void setNavigationDescription(CharSequence description) {
if (!TextUtils.isEmpty(description)) {
ensureNavButtonView();
}
if (mNavButtonView != null) {
mNavButtonView.setContentDescription(description);
}
}
/**
* 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.
*
* <p>Applications that wish to populate the toolbar's menu can do so from here. To use
* an XML menu resource, use {@link #inflateMenu(int)}.</p>
*
* @return The toolbar's Menu
*/
public Menu getMenu() {
ensureMenuView();
return mMenuView.getMenu();
}
private void ensureMenuView() {
if (mMenuView == null) {
mMenuView = new ActionMenuView(getContext());
mMenuView.setOnMenuItemClickListener(mMenuViewItemClickListener);
final LayoutParams lp = generateDefaultLayoutParams();
lp.gravity = Gravity.END | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
mMenuView.setLayoutParams(lp);
addSystemView(mMenuView);
}
}
private MenuInflater getMenuInflater() {
return new MenuInflater(getContext());
}
/**
* Inflate a menu resource into this toolbar.
*
* <p>Inflate an XML menu resource into this toolbar. Existing items in the menu will not
* be modified or removed.</p>
*
* @param resId ID of a menu resource to inflate
*/
public void inflateMenu(int resId) {
getMenuInflater().inflate(resId, getMenu());
}
/**
* Set a listener to respond to menu item click events.
*
* <p>This listener will be invoked whenever a user selects a menu item from
* the action buttons presented at the end of the toolbar or the associated overflow.</p>
*
* @param listener Listener to set
*/
public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
mOnMenuItemClickListener = listener;
}
/**
* Set the content insets for this toolbar relative to layout direction.
*
* <p>The content inset affects the valid area for Toolbar content other than
* the navigation button and menu. Insets define the minimum margin for these components
* and can be used to effectively align Toolbar content along well-known gridlines.</p>
*
* @param contentInsetStart Content inset for the toolbar starting edge
* @param contentInsetEnd Content inset for the toolbar ending edge
*
* @see #setContentInsetsAbsolute(int, int)
* @see #getContentInsetStart()
* @see #getContentInsetEnd()
* @see #getContentInsetLeft()
* @see #getContentInsetRight()
*/
public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) {
mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
}
/**
* Get the starting content inset for this toolbar.
*
* <p>The content inset affects the valid area for Toolbar content other than
* the navigation button and menu. Insets define the minimum margin for these components
* and can be used to effectively align Toolbar content along well-known gridlines.</p>
*
* @return The starting content inset for this toolbar
*
* @see #setContentInsetsRelative(int, int)
* @see #setContentInsetsAbsolute(int, int)
* @see #getContentInsetEnd()
* @see #getContentInsetLeft()
* @see #getContentInsetRight()
*/
public int getContentInsetStart() {
return mContentInsets.getStart();
}
/**
* Get the ending content inset for this toolbar.
*
* <p>The content inset affects the valid area for Toolbar content other than
* the navigation button and menu. Insets define the minimum margin for these components
* and can be used to effectively align Toolbar content along well-known gridlines.</p>
*
* @return The ending content inset for this toolbar
*
* @see #setContentInsetsRelative(int, int)
* @see #setContentInsetsAbsolute(int, int)
* @see #getContentInsetStart()
* @see #getContentInsetLeft()
* @see #getContentInsetRight()
*/
public int getContentInsetEnd() {
return mContentInsets.getEnd();
}
/**
* Set the content insets for this toolbar.
*
* <p>The content inset affects the valid area for Toolbar content other than
* the navigation button and menu. Insets define the minimum margin for these components
* and can be used to effectively align Toolbar content along well-known gridlines.</p>
*
* @param contentInsetLeft Content inset for the toolbar's left edge
* @param contentInsetRight Content inset for the toolbar's right edge
*
* @see #setContentInsetsAbsolute(int, int)
* @see #getContentInsetStart()
* @see #getContentInsetEnd()
* @see #getContentInsetLeft()
* @see #getContentInsetRight()
*/
public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) {
mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
}
/**
* Get the left content inset for this toolbar.
*
* <p>The content inset affects the valid area for Toolbar content other than
* the navigation button and menu. Insets define the minimum margin for these components
* and can be used to effectively align Toolbar content along well-known gridlines.</p>
*
* @return The left content inset for this toolbar
*
* @see #setContentInsetsRelative(int, int)
* @see #setContentInsetsAbsolute(int, int)
* @see #getContentInsetStart()
* @see #getContentInsetEnd()
* @see #getContentInsetRight()
*/
public int getContentInsetLeft() {
return mContentInsets.getLeft();
}
/**
* Get the right content inset for this toolbar.
*
* <p>The content inset affects the valid area for Toolbar content other than
* the navigation button and menu. Insets define the minimum margin for these components
* and can be used to effectively align Toolbar content along well-known gridlines.</p>
*
* @return The right content inset for this toolbar
*
* @see #setContentInsetsRelative(int, int)
* @see #setContentInsetsAbsolute(int, int)
* @see #getContentInsetStart()
* @see #getContentInsetEnd()
* @see #getContentInsetLeft()
*/
public int getContentInsetRight() {
return mContentInsets.getRight();
}
private void ensureNavButtonView() {
if (mNavButtonView == null) {
mNavButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
final LayoutParams lp = generateDefaultLayoutParams();
lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
mNavButtonView.setLayoutParams(lp);
}
}
private void ensureCollapseButtonView() {
if (mCollapseButtonView == null) {
mCollapseButtonView = new ImageButton(getContext(), null, 0, mNavButtonStyle);
mCollapseButtonView.setImageDrawable(mCollapseIcon);
final LayoutParams lp = generateDefaultLayoutParams();
lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
lp.mViewType = LayoutParams.EXPANDED;
mCollapseButtonView.setLayoutParams(lp);
mCollapseButtonView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
collapseActionView();
}
});
}
}
private void addSystemView(View v) {
final LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.mViewType = LayoutParams.SYSTEM;
addView(v, lp);
}
@Override
protected Parcelable onSaveInstanceState() {
SavedState state = new SavedState(super.onSaveInstanceState());
return state;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
final SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
}
private void measureChildConstrained(View child, int parentWidthSpec, int widthUsed,
int parentHeightSpec, int heightUsed, int heightConstraint) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidthSpec = getChildMeasureSpec(parentWidthSpec,
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
int childHeightSpec = getChildMeasureSpec(parentHeightSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
final int childHeightMode = MeasureSpec.getMode(childHeightSpec);
if (childHeightMode != MeasureSpec.EXACTLY && heightConstraint >= 0) {
final int size = childHeightMode != MeasureSpec.UNSPECIFIED ?
Math.min(MeasureSpec.getSize(childHeightSpec), heightConstraint) :
heightConstraint;
childHeightSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
}
child.measure(childWidthSpec, childHeightSpec);
}
/**
* Returns the width + uncollapsed margins
*/
private int measureChildCollapseMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed, int[] collapsingMargins) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int leftDiff = lp.leftMargin - collapsingMargins[0];
final int rightDiff = lp.rightMargin - collapsingMargins[1];
final int leftMargin = Math.max(0, leftDiff);
final int rightMargin = Math.max(0, rightDiff);
final int hMargins = leftMargin + rightMargin;
collapsingMargins[0] = Math.max(0, -leftDiff);
collapsingMargins[1] = Math.max(0, -rightDiff);
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight + hMargins + widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
return child.getMeasuredWidth() + hMargins;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0;
int height = 0;
int childState = 0;
final int[] collapsingMargins = mTempMargins;
final int marginStartIndex;
final int marginEndIndex;
if (isLayoutRtl()) {
marginStartIndex = 1;
marginEndIndex = 0;
} else {
marginStartIndex = 0;
marginEndIndex = 1;
}
// System views measure first.
int navWidth = 0;
if (shouldLayout(mNavButtonView)) {
measureChildConstrained(mNavButtonView, widthMeasureSpec, width, heightMeasureSpec, 0,
mMaxButtonHeight);
navWidth = mNavButtonView.getMeasuredWidth() + getHorizontalMargins(mNavButtonView);
height = Math.max(height, mNavButtonView.getMeasuredHeight() +
getVerticalMargins(mNavButtonView));
childState = combineMeasuredStates(childState, mNavButtonView.getMeasuredState());
}
if (shouldLayout(mCollapseButtonView)) {
measureChildConstrained(mCollapseButtonView, widthMeasureSpec, width,
heightMeasureSpec, 0, mMaxButtonHeight);
navWidth = mCollapseButtonView.getMeasuredWidth() +
getHorizontalMargins(mCollapseButtonView);
height = Math.max(height, mCollapseButtonView.getMeasuredHeight() +
getVerticalMargins(mCollapseButtonView));
childState = combineMeasuredStates(childState, mCollapseButtonView.getMeasuredState());
}
final int contentInsetStart = getContentInsetStart();
width += Math.max(contentInsetStart, navWidth);
collapsingMargins[marginStartIndex] = Math.max(0, contentInsetStart - navWidth);
int menuWidth = 0;
if (shouldLayout(mMenuView)) {
measureChildConstrained(mMenuView, widthMeasureSpec, width, heightMeasureSpec, 0,
mMaxButtonHeight);
menuWidth = mMenuView.getMeasuredWidth() + getHorizontalMargins(mMenuView);
height = Math.max(height, mMenuView.getMeasuredHeight() +
getVerticalMargins(mMenuView));
childState = combineMeasuredStates(childState, mMenuView.getMeasuredState());
}
final int contentInsetEnd = getContentInsetEnd();
width += Math.max(contentInsetEnd, menuWidth);
collapsingMargins[marginEndIndex] = Math.max(0, contentInsetEnd - menuWidth);
if (shouldLayout(mExpandedActionView)) {
width += measureChildCollapseMargins(mExpandedActionView, widthMeasureSpec, width,
heightMeasureSpec, 0, collapsingMargins);
height = Math.max(height, mExpandedActionView.getMeasuredHeight() +
getVerticalMargins(mExpandedActionView));
childState = combineMeasuredStates(childState, mExpandedActionView.getMeasuredState());
}
if (shouldLayout(mLogoView)) {
width += measureChildCollapseMargins(mLogoView, widthMeasureSpec, width,
heightMeasureSpec, 0, collapsingMargins);
height = Math.max(height, mLogoView.getMeasuredHeight() +
getVerticalMargins(mLogoView));
childState = combineMeasuredStates(childState, mLogoView.getMeasuredState());
}
int titleWidth = 0;
int titleHeight = 0;
final int titleVertMargins = mTitleMarginTop + mTitleMarginBottom;
final int titleHorizMargins = mTitleMarginStart + mTitleMarginEnd;
if (shouldLayout(mTitleTextView)) {
titleWidth = measureChildCollapseMargins(mTitleTextView, widthMeasureSpec,
width + titleHorizMargins, heightMeasureSpec, titleVertMargins,
collapsingMargins);
titleWidth = mTitleTextView.getMeasuredWidth() + getHorizontalMargins(mTitleTextView);
titleHeight = mTitleTextView.getMeasuredHeight() + getVerticalMargins(mTitleTextView);
childState = combineMeasuredStates(childState, mTitleTextView.getMeasuredState());
}
if (shouldLayout(mSubtitleTextView)) {
titleWidth = Math.max(titleWidth, measureChildCollapseMargins(mSubtitleTextView,
widthMeasureSpec, width + titleHorizMargins,
heightMeasureSpec, titleHeight + titleVertMargins,
collapsingMargins));
titleHeight += mSubtitleTextView.getMeasuredHeight() +
getVerticalMargins(mSubtitleTextView);
childState = combineMeasuredStates(childState, mSubtitleTextView.getMeasuredState());
}
width += titleWidth;
height = Math.max(height, titleHeight);
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.mViewType != LayoutParams.CUSTOM || !shouldLayout(child)) {
// We already got all system views above. Skip them and GONE views.
continue;
}
width += measureChildCollapseMargins(child, widthMeasureSpec, width,
heightMeasureSpec, 0, collapsingMargins);
height = Math.max(height, child.getMeasuredHeight() + getVerticalMargins(child));
childState = combineMeasuredStates(childState, child.getMeasuredState());
}
// Measurement already took padding into account for available space for the children,
// add it in for the final size.
width += getPaddingLeft() + getPaddingRight();
height += getPaddingTop() + getPaddingBottom();
final int measuredWidth = resolveSizeAndState(
Math.max(width, getSuggestedMinimumWidth()),
widthMeasureSpec, childState & MEASURED_STATE_MASK);
final int measuredHeight = resolveSizeAndState(
Math.max(height, getSuggestedMinimumHeight()),
heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT);
setMeasuredDimension(measuredWidth, measuredHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
final int width = getWidth();
final int height = getHeight();
final int paddingLeft = getPaddingLeft();
final int paddingRight = getPaddingRight();
final int paddingTop = getPaddingTop();
final int paddingBottom = getPaddingBottom();
int left = paddingLeft;
int right = width - paddingRight;
final int[] collapsingMargins = mTempMargins;
collapsingMargins[0] = collapsingMargins[1] = 0;
if (shouldLayout(mNavButtonView)) {
if (isRtl) {
right = layoutChildRight(mNavButtonView, right, collapsingMargins);
} else {
left = layoutChildLeft(mNavButtonView, left, collapsingMargins);
}
}
if (shouldLayout(mCollapseButtonView)) {
if (isRtl) {
right = layoutChildRight(mCollapseButtonView, right, collapsingMargins);
} else {
left = layoutChildLeft(mCollapseButtonView, left, collapsingMargins);
}
}
if (shouldLayout(mMenuView)) {
if (isRtl) {
left = layoutChildLeft(mMenuView, left, collapsingMargins);
} else {
right = layoutChildRight(mMenuView, right, collapsingMargins);
}
}
collapsingMargins[0] = Math.max(0, getContentInsetLeft() - left);
collapsingMargins[1] = Math.max(0, getContentInsetRight() - (width - paddingRight - right));
left = Math.max(left, getContentInsetLeft());
right = Math.min(right, width - paddingRight - getContentInsetRight());
if (shouldLayout(mExpandedActionView)) {
if (isRtl) {
right = layoutChildRight(mExpandedActionView, right, collapsingMargins);
} else {
left = layoutChildLeft(mExpandedActionView, left, collapsingMargins);
}
}
if (shouldLayout(mLogoView)) {
if (isRtl) {
right = layoutChildRight(mLogoView, right, collapsingMargins);
} else {
left = layoutChildLeft(mLogoView, left, collapsingMargins);
}
}
final boolean layoutTitle = shouldLayout(mTitleTextView);
final boolean layoutSubtitle = shouldLayout(mSubtitleTextView);
int titleHeight = 0;
if (layoutTitle) {
final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
titleHeight += lp.topMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
}
if (layoutSubtitle) {
final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
titleHeight += lp.bottomMargin + mTitleTextView.getMeasuredHeight() + lp.bottomMargin;
}
if (layoutTitle || layoutSubtitle) {
int titleTop;
final View topChild = layoutTitle ? mTitleTextView : mSubtitleTextView;
final View bottomChild = layoutSubtitle ? mSubtitleTextView : mTitleTextView;
final LayoutParams toplp = (LayoutParams) topChild.getLayoutParams();
final LayoutParams bottomlp = (LayoutParams) bottomChild.getLayoutParams();
switch (mGravity & Gravity.VERTICAL_GRAVITY_MASK) {
case Gravity.TOP:
titleTop = getPaddingTop() + toplp.topMargin + mTitleMarginTop;
break;
default:
case Gravity.CENTER_VERTICAL:
final int space = height - paddingTop - paddingBottom;
int spaceAbove = (space - titleHeight) / 2;
if (spaceAbove < toplp.topMargin + mTitleMarginTop) {
spaceAbove = toplp.topMargin + mTitleMarginTop;
} else {
final int spaceBelow = height - paddingBottom - titleHeight -
spaceAbove - paddingTop;
if (spaceBelow < toplp.bottomMargin + mTitleMarginBottom) {
spaceAbove = Math.max(0, spaceAbove -
(bottomlp.bottomMargin + mTitleMarginBottom - spaceBelow));
}
}
titleTop = paddingTop + spaceAbove;
break;
case Gravity.BOTTOM:
titleTop = height - paddingBottom - bottomlp.bottomMargin - mTitleMarginBottom -
titleHeight;
break;
}
if (isRtl) {
final int rd = mTitleMarginStart - collapsingMargins[1];
right -= Math.max(0, rd);
collapsingMargins[1] = Math.max(0, -rd);
int titleRight = right;
int subtitleRight = right;
if (layoutTitle) {
final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
final int titleLeft = titleRight - mTitleTextView.getMeasuredWidth();
final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
titleRight = titleLeft - mTitleMarginEnd;
titleTop = titleBottom + lp.bottomMargin;
}
if (layoutSubtitle) {
final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
titleTop += lp.topMargin;
final int subtitleLeft = subtitleRight - mSubtitleTextView.getMeasuredWidth();
final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
subtitleRight = subtitleRight - mTitleMarginEnd;
titleTop = subtitleBottom + lp.bottomMargin;
}
right = Math.max(titleRight, subtitleRight);
} else {
final int ld = mTitleMarginStart - collapsingMargins[0];
left += Math.max(0, ld);
collapsingMargins[0] = Math.max(0, -ld);
int titleLeft = left;
int subtitleLeft = left;
if (layoutTitle) {
final LayoutParams lp = (LayoutParams) mTitleTextView.getLayoutParams();
final int titleRight = titleLeft + mTitleTextView.getMeasuredWidth();
final int titleBottom = titleTop + mTitleTextView.getMeasuredHeight();
mTitleTextView.layout(titleLeft, titleTop, titleRight, titleBottom);
titleLeft = titleRight + mTitleMarginEnd;
titleTop = titleBottom + lp.bottomMargin;
}
if (layoutSubtitle) {
final LayoutParams lp = (LayoutParams) mSubtitleTextView.getLayoutParams();
titleTop += lp.topMargin;
final int subtitleRight = subtitleLeft + mSubtitleTextView.getMeasuredWidth();
final int subtitleBottom = titleTop + mSubtitleTextView.getMeasuredHeight();
mSubtitleTextView.layout(subtitleLeft, titleTop, subtitleRight, subtitleBottom);
subtitleLeft = subtitleRight + mTitleMarginEnd;
titleTop = subtitleBottom + lp.bottomMargin;
}
left = Math.max(titleLeft, subtitleLeft);
}
}
// Get all remaining children sorted for layout. This is all prepared
// such that absolute layout direction can be used below.
addCustomViewsWithGravity(mTempViews, Gravity.LEFT);
final int leftViewsCount = mTempViews.size();
for (int i = 0; i < leftViewsCount; i++) {
left = layoutChildLeft(mTempViews.get(i), left, collapsingMargins);
}
addCustomViewsWithGravity(mTempViews, Gravity.RIGHT);
final int rightViewsCount = mTempViews.size();
for (int i = 0; i < rightViewsCount; i++) {
right = layoutChildRight(mTempViews.get(i), right, collapsingMargins);
}
// Centered views try to center with respect to the whole bar, but views pinned
// to the left or right can push the mass of centered views to one side or the other.
addCustomViewsWithGravity(mTempViews, Gravity.CENTER_HORIZONTAL);
final int centerViewsWidth = getViewListMeasuredWidth(mTempViews, collapsingMargins);
final int parentCenter = paddingLeft + (width - paddingLeft - paddingRight) / 2;
final int halfCenterViewsWidth = centerViewsWidth / 2;
int centerLeft = parentCenter - halfCenterViewsWidth;
final int centerRight = centerLeft + centerViewsWidth;
if (centerLeft < left) {
centerLeft = left;
} else if (centerRight > right) {
centerLeft -= centerRight - right;
}
final int centerViewsCount = mTempViews.size();
for (int i = 0; i < centerViewsCount; i++) {
centerLeft = layoutChildLeft(mTempViews.get(i), centerLeft, collapsingMargins);
}
mTempViews.clear();
}
private int getViewListMeasuredWidth(List<View> views, int[] collapsingMargins) {
int collapseLeft = collapsingMargins[0];
int collapseRight = collapsingMargins[1];
int width = 0;
final int count = views.size();
for (int i = 0; i < count; i++) {
final View v = views.get(i);
final LayoutParams lp = (LayoutParams) v.getLayoutParams();
final int l = lp.leftMargin - collapseLeft;
final int r = lp.rightMargin - collapseRight;
final int leftMargin = Math.max(0, l);
final int rightMargin = Math.max(0, r);
collapseLeft = Math.max(0, -l);
collapseRight = Math.max(0, -r);
width += leftMargin + v.getMeasuredWidth() + rightMargin;
}
return width;
}
private int layoutChildLeft(View child, int left, int[] collapsingMargins) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int l = lp.leftMargin - collapsingMargins[0];
left += Math.max(0, l);
collapsingMargins[0] = Math.max(0, -l);
final int top = getChildTop(child);
final int childWidth = child.getMeasuredWidth();
child.layout(left, top, left + childWidth, top + child.getMeasuredHeight());
left += childWidth + lp.rightMargin;
return left;
}
private int layoutChildRight(View child, int right, int[] collapsingMargins) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int r = lp.rightMargin - collapsingMargins[1];
right -= Math.max(0, r);
collapsingMargins[1] = Math.max(0, -r);
final int top = getChildTop(child);
final int childWidth = child.getMeasuredWidth();
child.layout(right - childWidth, top, right, top + child.getMeasuredHeight());
right -= childWidth + lp.leftMargin;
return right;
}
private int getChildTop(View child) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
switch (getChildVerticalGravity(lp.gravity)) {
case Gravity.TOP:
return getPaddingTop();
case Gravity.BOTTOM:
return getPaddingBottom() - child.getMeasuredHeight() - lp.bottomMargin;
default:
case Gravity.CENTER_VERTICAL:
final int paddingTop = getPaddingTop();
final int paddingBottom = getPaddingBottom();
final int height = getHeight();
final int childHeight = child.getMeasuredHeight();
final int space = height - paddingTop - paddingBottom;
int spaceAbove = (space - childHeight) / 2;
if (spaceAbove < lp.topMargin) {
spaceAbove = lp.topMargin;
} else {
final int spaceBelow = height - paddingBottom - childHeight -
spaceAbove - paddingTop;
if (spaceBelow < lp.bottomMargin) {
spaceAbove = Math.max(0, spaceAbove - (lp.bottomMargin - spaceBelow));
}
}
return paddingTop + spaceAbove;
}
}
private int getChildVerticalGravity(int gravity) {
final int vgrav = gravity & Gravity.VERTICAL_GRAVITY_MASK;
switch (vgrav) {
case Gravity.TOP:
case Gravity.BOTTOM:
case Gravity.CENTER_VERTICAL:
return vgrav;
default:
return mGravity & Gravity.VERTICAL_GRAVITY_MASK;
}
}
/**
* Prepare a list of non-SYSTEM child views. If the layout direction is RTL
* this will be in reverse child order.
*
* @param views List to populate. It will be cleared before use.
* @param gravity Horizontal gravity to match against
*/
private void addCustomViewsWithGravity(List<View> views, int gravity) {
final boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
final int childCount = getChildCount();
final int absGrav = Gravity.getAbsoluteGravity(gravity, getLayoutDirection());
views.clear();
if (isRtl) {
for (int i = childCount - 1; i >= 0; i--) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
getChildHorizontalGravity(lp.gravity) == absGrav) {
views.add(child);
}
}
} else {
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child) &&
getChildHorizontalGravity(lp.gravity) == absGrav) {
views.add(child);
}
}
}
}
private int getChildHorizontalGravity(int gravity) {
final int ld = getLayoutDirection();
final int absGrav = Gravity.getAbsoluteGravity(gravity, ld);
final int hGrav = absGrav & Gravity.HORIZONTAL_GRAVITY_MASK;
switch (hGrav) {
case Gravity.LEFT:
case Gravity.RIGHT:
case Gravity.CENTER_HORIZONTAL:
return hGrav;
default:
return ld == LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT;
}
}
private boolean shouldLayout(View view) {
return view != null && view.getParent() == this && view.getVisibility() != GONE;
}
private int getHorizontalMargins(View v) {
final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
return mlp.getMarginStart() + mlp.getMarginEnd();
}
private int getVerticalMargins(View v) {
final MarginLayoutParams mlp = (MarginLayoutParams) v.getLayoutParams();
return mlp.topMargin + mlp.bottomMargin;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
if (p instanceof LayoutParams) {
return new LayoutParams((LayoutParams) p);
} else if (p instanceof ActionBar.LayoutParams) {
return new LayoutParams((ActionBar.LayoutParams) p);
} else if (p instanceof MarginLayoutParams) {
return new LayoutParams((MarginLayoutParams) p);
} else {
return new LayoutParams(p);
}
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return super.checkLayoutParams(p) && p instanceof LayoutParams;
}
private static boolean isCustomView(View child) {
return ((LayoutParams) child.getLayoutParams()).mViewType == LayoutParams.CUSTOM;
}
/** @hide */
public DecorToolbar getWrapper() {
if (mWrapper == null) {
mWrapper = new ToolbarWidgetWrapper(this);
}
return mWrapper;
}
private void setChildVisibilityForExpandedActionView(boolean expand) {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.mViewType != LayoutParams.EXPANDED && child != mMenuView) {
child.setVisibility(expand ? GONE : VISIBLE);
}
}
}
/**
* Interface responsible for receiving menu item click events if the items themselves
* do not have individual item click listeners.
*/
public interface OnMenuItemClickListener {
/**
* This method will be invoked when a menu item is clicked if the item itself did
* not already handle the event.
*
* @param item {@link MenuItem} that was clicked
* @return <code>true</code> if the event was handled, <code>false</code> otherwise.
*/
public boolean onMenuItemClick(MenuItem item);
}
/**
* Layout information for child views of Toolbars.
*
* @attr ref android.R.styleable#Toolbar_LayoutParams_layout_gravity
*/
public static class LayoutParams extends ActionBar.LayoutParams {
static final int CUSTOM = 0;
static final int SYSTEM = 1;
static final int EXPANDED = 2;
int mViewType = CUSTOM;
public LayoutParams(@NonNull Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
this.gravity = Gravity.CENTER_VERTICAL | Gravity.START;
}
public LayoutParams(int width, int height, int gravity) {
super(width, height);
this.gravity = gravity;
}
public LayoutParams(int gravity) {
this(WRAP_CONTENT, MATCH_PARENT, gravity);
}
public LayoutParams(LayoutParams source) {
super(source);
mViewType = source.mViewType;
}
public LayoutParams(ActionBar.LayoutParams source) {
super(source);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
}
static class SavedState extends BaseSavedState {
public SavedState(Parcel source) {
super(source);
}
public SavedState(Parcelable superState) {
super(superState);
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
}
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel source) {
return new SavedState(source);
}
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
private class ExpandedActionViewMenuPresenter implements MenuPresenter {
MenuBuilder mMenu;
MenuItemImpl mCurrentExpandedItem;
@Override
public void initForMenu(Context context, MenuBuilder menu) {
// Clear the expanded action view when menus change.
if (mMenu != null && mCurrentExpandedItem != null) {
mMenu.collapseItemActionView(mCurrentExpandedItem);
}
mMenu = menu;
}
@Override
public MenuView getMenuView(ViewGroup root) {
return null;
}
@Override
public void updateMenuView(boolean cleared) {
// Make sure the expanded item we have is still there.
if (mCurrentExpandedItem != null) {
boolean found = false;
if (mMenu != null) {
final int count = mMenu.size();
for (int i = 0; i < count; i++) {
final MenuItem item = mMenu.getItem(i);
if (item == mCurrentExpandedItem) {
found = true;
break;
}
}
}
if (!found) {
// The item we had expanded disappeared. Collapse.
collapseItemActionView(mMenu, mCurrentExpandedItem);
}
}
}
@Override
public void setCallback(Callback cb) {
}
@Override
public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
return false;
}
@Override
public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
}
@Override
public boolean flagActionItems() {
return false;
}
@Override
public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
ensureCollapseButtonView();
if (mCollapseButtonView.getParent() != Toolbar.this) {
addView(mCollapseButtonView);
}
mExpandedActionView = item.getActionView();
mCurrentExpandedItem = item;
if (mExpandedActionView.getParent() != Toolbar.this) {
final LayoutParams lp = generateDefaultLayoutParams();
lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
lp.mViewType = LayoutParams.EXPANDED;
mExpandedActionView.setLayoutParams(lp);
addView(mExpandedActionView);
}
setChildVisibilityForExpandedActionView(true);
requestLayout();
item.setActionViewExpanded(true);
if (mExpandedActionView instanceof CollapsibleActionView) {
((CollapsibleActionView) mExpandedActionView).onActionViewExpanded();
}
return true;
}
@Override
public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
// Do this before detaching the actionview from the hierarchy, in case
// it needs to dismiss the soft keyboard, etc.
if (mExpandedActionView instanceof CollapsibleActionView) {
((CollapsibleActionView) mExpandedActionView).onActionViewCollapsed();
}
removeView(mExpandedActionView);
removeView(mCollapseButtonView);
mExpandedActionView = null;
setChildVisibilityForExpandedActionView(false);
mCurrentExpandedItem = null;
requestLayout();
item.setActionViewExpanded(false);
return true;
}
@Override
public int getId() {
return 0;
}
@Override
public Parcelable onSaveInstanceState() {
return null;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
}
}
}
|