Skip to content

Commit ea01f2a

Browse files
imhappipaulfthomas
authored andcommitted
[Searchbar] Fix centering text when set by the search view
PiperOrigin-RevId: 735817759
1 parent 6f41625 commit ea01f2a

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

catalog/java/io/material/catalog/search/SearchDemoUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private static List<SuggestionItem> getThisWeekSuggestions() {
184184

185185
private static void submitSearchQuery(SearchBar searchBar, SearchView searchView, String query) {
186186
searchBar.setText(query);
187-
searchView.hide();
187+
searchBar.post(() -> searchView.hide());
188188
}
189189

190190
private static class SuggestionItem {

catalog/java/io/material/catalog/search/res/layout/cat_search_appbar_icons_fragment.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
xmlns:android="http://schemas.android.com/apk/res/android"
1919
xmlns:app="http://schemas.android.com/apk/res-auto"
2020
android:layout_width="match_parent"
21-
android:layout_height="match_parent"
22-
android:fitsSystemWindows="true">
21+
android:layout_height="match_parent">
2322

2423
<com.google.android.material.appbar.AppBarLayout
2524
android:id="@+id/app_bar_layout"
@@ -76,6 +75,7 @@
7675
android:layout_width="match_parent"
7776
android:layout_height="wrap_content"
7877
android:layout_margin="16dp"
78+
android:fitsSystemWindows="true"
7979
android:lineSpacingMultiplier="1.2"
8080
android:text="@string/cat_searchbar_lorem_ipsum"/>
8181
</androidx.core.widget.NestedScrollView>

catalog/java/io/material/catalog/search/res/layout/cat_search_fragment.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
xmlns:android="http://schemas.android.com/apk/res/android"
1919
xmlns:app="http://schemas.android.com/apk/res-auto"
2020
android:layout_width="match_parent"
21-
android:layout_height="match_parent">
21+
android:layout_height="match_parent"
22+
android:fitsSystemWindows="true">
2223

2324
<androidx.core.widget.NestedScrollView
2425
android:id="@+id/nested_scroll_view"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,10 @@ public void setTextCentered(boolean textCentered) {
608608
}
609609
Toolbar.LayoutParams lp = (LayoutParams) textView.getLayoutParams();
610610
if (textCentered) {
611+
textView.setGravity(Gravity.CENTER_HORIZONTAL);
611612
lp.gravity = Gravity.CENTER_HORIZONTAL;
612613
} else {
614+
textView.setGravity(Gravity.NO_GRAVITY);
613615
lp.gravity = Gravity.NO_GRAVITY;
614616
}
615617
textView.setLayoutParams(lp);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import androidx.appcompat.graphics.drawable.DrawerArrowDrawable;
3131
import androidx.appcompat.widget.ActionMenuView;
3232
import androidx.appcompat.widget.Toolbar;
33+
import android.text.TextUtils;
3334
import android.view.Menu;
3435
import android.view.View;
3536
import android.view.ViewGroup.MarginLayoutParams;
@@ -550,8 +551,22 @@ private Animator getEditTextAnimator(boolean show) {
550551
}
551552

552553
private Animator getTranslationAnimatorForText(boolean show, View v) {
554+
TextView textView = searchBar.getTextView();
555+
int additionalMovement = 0;
556+
// If the text is centered and the text's hint is not equal to the text (ie. if there's any
557+
// extra space in between the textview's start and the actual text bounds)
558+
if (!TextUtils.isEmpty(textView.getText())
559+
&& searchBar.getTextCentered()
560+
&& textView.getHint() != textView.getText()) {
561+
String text = textView.getText().toString();
562+
Rect bounds = new Rect();
563+
564+
textView.getPaint().getTextBounds(text, 0, text.length(), bounds);
565+
additionalMovement = max(0, searchBar.getTextView().getMeasuredWidth() / 2 - bounds.width() / 2);
566+
}
553567
int startX =
554568
searchBar.getTextView().getLeft()
569+
+ additionalMovement
555570
+ searchBar.getLeft()
556571
- (v.getLeft() + textContainer.getLeft());
557572
return getTranslationAnimator(show, v, startX, getFromTranslationY());

0 commit comments

Comments
 (0)