@@ -146,8 +146,6 @@ public class SearchBar extends Toolbar {
146
146
private final Drawable defaultNavigationIcon ;
147
147
private final boolean tintNavigationIcon ;
148
148
private final boolean forceDefaultNavigationOnClickListener ;
149
- private final int minimumTextMarginStart ;
150
- private int currentTextMarginStart ;
151
149
@ Nullable private View centerView ;
152
150
@ Nullable private Integer navigationIconTint ;
153
151
@ Nullable private Drawable originalNavigationIconBackground ;
@@ -157,6 +155,7 @@ public class SearchBar extends Toolbar {
157
155
private boolean textCentered ;
158
156
private int maxWidth ;
159
157
@ Nullable private ActionMenuView menuView ;
158
+ @ Nullable private ImageButton navIconButton ;
160
159
161
160
private final LiftOnScrollProgressListener liftColorListener =
162
161
new LiftOnScrollProgressListener () {
@@ -185,9 +184,6 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
185
184
context = getContext ();
186
185
validateAttributes (attrs );
187
186
188
- minimumTextMarginStart =
189
- getResources ()
190
- .getDimensionPixelSize (R .dimen .m3_searchbar_text_margin_start_no_navigation_icon );
191
187
defaultNavigationIcon =
192
188
AppCompatResources .getDrawable (context , getDefaultNavigationIconResource ());
193
189
searchBarAnimationHelper = new SearchBarAnimationHelper ();
@@ -427,7 +423,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
427
423
super .onLayout (changed , left , top , right , bottom );
428
424
429
425
if (centerView != null ) {
430
- layoutViewInCenter (centerView , /* overlapMenu = */ true );
426
+ layoutViewInCenter (centerView , /* absolutePlacement = */ true );
431
427
}
432
428
setHandwritingBoundsInsets ();
433
429
if (textView != null ) {
@@ -436,37 +432,9 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
436
432
// to be pushed to the side. In this case, we want the textview to be still centered on top of
437
433
// any center views.
438
434
if (textCentered ) {
439
- layoutViewInCenter (textView , /* overlapMenu = */ false );
435
+ layoutViewInCenter (textView , /* absolutePlacement = */ false );
440
436
}
441
- // If after laying out, there's not enough space between the textview and the start of
442
- // the SearchBar, we add a margin.
443
- Toolbar .LayoutParams lp = (LayoutParams ) textView .getLayoutParams ();
444
- int currentMargin = lp .getMarginStart ();
445
- int newMargin = getNewMargin (currentMargin );
446
- if (currentMargin != newMargin ) {
447
- lp .setMarginStart (newMargin );
448
- currentTextMarginStart = newMargin ;
449
- textView .setLayoutParams (lp );
450
- }
451
- }
452
- }
453
-
454
- private int getNewMargin (int currentMargin ) {
455
- // The start position of the textview with respect to its parent, the searchbar.
456
- int textViewStart =
457
- getLayoutDirection () == LAYOUT_DIRECTION_RTL
458
- ? getMeasuredWidth () - textView .getRight ()
459
- : textView .getLeft ();
460
- int additionalMarginStart = max (minimumTextMarginStart - textViewStart , 0 );
461
- // If we are already including the margin in the textview start, and it is necessary to be added
462
- // (ie. keeping the desired distance between the textview and the start), we should keep
463
- // the margin.
464
- if (currentTextMarginStart != 0
465
- && currentMargin == currentTextMarginStart
466
- && (textViewStart - currentMargin ) < minimumTextMarginStart ) {
467
- additionalMarginStart = currentMargin ;
468
437
}
469
- return additionalMarginStart ;
470
438
}
471
439
472
440
/**
@@ -605,22 +573,30 @@ private ActionMenuView findOrGetMenuView() {
605
573
return menuView ;
606
574
}
607
575
576
+ @ Nullable
577
+ private ImageButton findOrGetNavView () {
578
+ if (navIconButton == null ) {
579
+ navIconButton = ToolbarUtils .getNavigationIconButton (this );
580
+ }
581
+ return navIconButton ;
582
+ }
583
+
608
584
/**
609
585
* Lays out the given view in the center of the {@link SearchBar}.
610
586
*
611
587
* @param view The view to layout in the center.
612
- * @param overlapMenu Whether the view can overlap the menu. This should be true if your centered
613
- * view should be absolute (eg. a product logo )
588
+ * @param absolutePlacement Whether the view will be placed absolutely in the center (eg. ignoring
589
+ * other toolbar elements like the nav icon and menu, padding, content insets )
614
590
*/
615
- private void layoutViewInCenter (View view , boolean overlapMenu ) {
591
+ private void layoutViewInCenter (View view , boolean absolutePlacement ) {
616
592
if (view == null ) {
617
593
return ;
618
594
}
619
595
620
596
int viewWidth = view .getMeasuredWidth ();
621
597
int left = getMeasuredWidth () / 2 - viewWidth / 2 ;
622
598
int right = left + viewWidth ;
623
- if (!overlapMenu ) {
599
+ if (!absolutePlacement ) {
624
600
View menuView = findOrGetMenuView ();
625
601
if (menuView != null ) {
626
602
int diff =
@@ -630,13 +606,37 @@ private void layoutViewInCenter(View view, boolean overlapMenu) {
630
606
left -= diff ;
631
607
right -= diff ;
632
608
}
633
- }
609
+ View navIcon = findOrGetNavView ();
610
+ if (navIcon != null ) {
611
+ int diff =
612
+ getLayoutDirection () == LAYOUT_DIRECTION_RTL
613
+ ? max (right - navIcon .getLeft (), 0 )
614
+ : max (navIcon .getRight () - left , 0 );
615
+ left += diff ;
616
+ right += diff ;
617
+ }
634
618
619
+ // Make sure to not lay out the view inside the SearchBar padding. paddingLeftAdded and
620
+ // paddingRightAdded will never be non-zero at the same time, as Toolbar.measure has already
621
+ // measured the children accounting for padding.
622
+ int paddingStartAdded = max (getPaddingStart () - left , getContentInsetStart () - left );
623
+ int paddingEndAdded =
624
+ max (
625
+ right - (getMeasuredWidth () - getPaddingEnd ()),
626
+ right - (getMeasuredWidth () - getContentInsetEnd ()));
627
+ left += max (paddingStartAdded , 0 ) - max (paddingEndAdded , 0 );
628
+ right += max (paddingStartAdded , 0 ) - max (paddingEndAdded , 0 );
629
+ }
635
630
int viewHeight = view .getMeasuredHeight ();
636
631
int top = getMeasuredHeight () / 2 - viewHeight / 2 ;
637
632
int bottom = top + viewHeight ;
638
633
639
- layoutChild (view , left , top , right , bottom );
634
+ layoutChild (
635
+ view ,
636
+ left ,
637
+ top ,
638
+ right ,
639
+ bottom );
640
640
}
641
641
642
642
private void layoutChild (View child , int left , int top , int right , int bottom ) {
0 commit comments