@@ -156,6 +156,7 @@ public class SearchBar extends Toolbar {
156
156
private MaterialShapeDrawable backgroundShape ;
157
157
private boolean textCentered ;
158
158
private int maxWidth ;
159
+ @ Nullable private ActionMenuView menuView ;
159
160
160
161
private final LiftOnScrollProgressListener liftColorListener =
161
162
new LiftOnScrollProgressListener () {
@@ -426,7 +427,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
426
427
super .onLayout (changed , left , top , right , bottom );
427
428
428
429
if (centerView != null ) {
429
- layoutViewInCenter (centerView );
430
+ layoutViewInCenter (centerView , /* overlapMenu= */ true );
430
431
}
431
432
setHandwritingBoundsInsets ();
432
433
if (textView != null ) {
@@ -435,7 +436,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
435
436
// to be pushed to the side. In this case, we want the textview to be still centered on top of
436
437
// any center views.
437
438
if (textCentered ) {
438
- layoutViewInCenter (textView );
439
+ layoutViewInCenter (textView , /* overlapMenu= */ false );
439
440
}
440
441
// If after laying out, there's not enough space between the textview and the start of
441
442
// the SearchBar, we add a margin.
@@ -596,14 +597,40 @@ private void measureCenterView(int widthMeasureSpec, int heightMeasureSpec) {
596
597
}
597
598
}
598
599
599
- private void layoutViewInCenter (View view ) {
600
+ @ Nullable
601
+ private ActionMenuView findOrGetMenuView () {
602
+ if (menuView == null ) {
603
+ menuView = ToolbarUtils .getActionMenuView (this );
604
+ }
605
+ return menuView ;
606
+ }
607
+
608
+ /**
609
+ * Lays out the given view in the center of the {@link SearchBar}.
610
+ *
611
+ * @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)
614
+ */
615
+ private void layoutViewInCenter (View view , boolean overlapMenu ) {
600
616
if (view == null ) {
601
617
return ;
602
618
}
603
619
604
620
int viewWidth = view .getMeasuredWidth ();
605
621
int left = getMeasuredWidth () / 2 - viewWidth / 2 ;
606
622
int right = left + viewWidth ;
623
+ if (!overlapMenu ) {
624
+ View menuView = findOrGetMenuView ();
625
+ if (menuView != null ) {
626
+ int diff =
627
+ getLayoutDirection () == LAYOUT_DIRECTION_RTL
628
+ ? max (menuView .getRight () - left , 0 )
629
+ : max (right - menuView .getLeft (), 0 );
630
+ left -= diff ;
631
+ right -= diff ;
632
+ }
633
+ }
607
634
608
635
int viewHeight = view .getMeasuredHeight ();
609
636
int top = getMeasuredHeight () / 2 - viewHeight / 2 ;
0 commit comments