Skip to content

Commit 9170221

Browse files
imhappidsn5ft
authored andcommitted
[SearchBar] Fix long centered text overlapping with menu items
PiperOrigin-RevId: 743214367
1 parent 005687d commit 9170221

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/java/com/google/android/material/search/SearchBar.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public class SearchBar extends Toolbar {
156156
private MaterialShapeDrawable backgroundShape;
157157
private boolean textCentered;
158158
private int maxWidth;
159+
@Nullable private ActionMenuView menuView;
159160

160161
private final LiftOnScrollProgressListener liftColorListener =
161162
new LiftOnScrollProgressListener() {
@@ -426,7 +427,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
426427
super.onLayout(changed, left, top, right, bottom);
427428

428429
if (centerView != null) {
429-
layoutViewInCenter(centerView);
430+
layoutViewInCenter(centerView, /* overlapMenu= */ true);
430431
}
431432
setHandwritingBoundsInsets();
432433
if (textView != null) {
@@ -435,7 +436,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
435436
// to be pushed to the side. In this case, we want the textview to be still centered on top of
436437
// any center views.
437438
if (textCentered) {
438-
layoutViewInCenter(textView);
439+
layoutViewInCenter(textView, /* overlapMenu= */ false);
439440
}
440441
// If after laying out, there's not enough space between the textview and the start of
441442
// the SearchBar, we add a margin.
@@ -596,14 +597,40 @@ private void measureCenterView(int widthMeasureSpec, int heightMeasureSpec) {
596597
}
597598
}
598599

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) {
600616
if (view == null) {
601617
return;
602618
}
603619

604620
int viewWidth = view.getMeasuredWidth();
605621
int left = getMeasuredWidth() / 2 - viewWidth / 2;
606622
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+
}
607634

608635
int viewHeight = view.getMeasuredHeight();
609636
int top = getMeasuredHeight() / 2 - viewHeight / 2;

0 commit comments

Comments
 (0)