Skip to content

Commit 0641ee5

Browse files
imhappipaulfthomas
authored andcommitted
[Searchbar] Add textCentered attribute
PiperOrigin-RevId: 733486317
1 parent 3938284 commit 0641ee5

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@
5050
android:layout_marginVertical="4dp"
5151
android:layout_gravity="center_vertical"
5252
app:defaultMarginsEnabled="false"
53+
app:textCentered="true"
5354
app:hideNavigationIcon="true"
54-
android:hint="@string/cat_searchbar_hint"/>
55+
android:hint="@string/cat_searchbar_short_hint"/>
5556
<ImageView
5657
android:layout_width="48dp"
5758
android:layout_height="48dp"
@@ -83,7 +84,7 @@
8384
android:id="@+id/cat_search_view"
8485
android:layout_width="match_parent"
8586
android:layout_height="match_parent"
86-
android:hint="@string/cat_searchbar_hint"
87+
android:hint="@string/cat_searchbar_short_hint"
8788
app:layout_anchor="@id/cat_search_bar">
8889

8990
<!-- Content goes here (ScrollView, RecyclerView, etc.). -->

catalog/java/io/material/catalog/search/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,6 @@
9292
description="Description about a home icon button.[CHAR_LIMIT=NONE]">Home</string>
9393
<string name="cat_searchbar_cast_icon_description"
9494
description="Description about a cast icon button.[CHAR_LIMIT=NONE]">Cast</string>
95-
95+
<string name="cat_searchbar_short_hint"
96+
description="Search bar short hint text to open search view [CHAR_LIMIT=40]">Search product</string>
9697
</resources>

docs/components/Search.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Element | Attribute | Related method(s)
105105
**Search text appearance** | `android:textAppearance` | `setTextAppearance`<br/>`getTextAppearance` | `@style/TextAppearance.Material3.SearchBar`
106106
**Search text** | `android:text` | `setText`<br/>`getText` | `null`
107107
**Search hint** | `android:hint` | `setHint`<br/>`getHint` | `null`
108+
**Search text centered** | `app:textCentered` | `setTextCentered`<br/>`getTextCentered` | `false`
108109
**Color** | `app:backgroundTint` | -- | `?attr/colorSurfaceContainerHigh`
109110
**Flag for default margins** | `app:defaultMarginsEnabled` | -- | `true`
110111
**Flag for navigation icon** | `app:hideNavigationIcon` | -- | `false`

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

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
2222
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
23+
import static java.lang.Math.max;
2324

2425
import android.animation.AnimatorListenerAdapter;
2526
import android.content.Context;
@@ -40,6 +41,7 @@
4041
import androidx.appcompat.widget.Toolbar;
4142
import android.text.TextUtils;
4243
import android.util.AttributeSet;
44+
import android.view.Gravity;
4345
import android.view.LayoutInflater;
4446
import android.view.Menu;
4547
import android.view.View;
@@ -138,12 +140,15 @@ public class SearchBar extends Toolbar {
138140
private final Drawable defaultNavigationIcon;
139141
private final boolean tintNavigationIcon;
140142
private final boolean forceDefaultNavigationOnClickListener;
143+
private final int minimumTextMarginStart;
144+
private int currentTextMarginStart;
141145
@Nullable private View centerView;
142146
@Nullable private Integer navigationIconTint;
143147
@Nullable private Drawable originalNavigationIconBackground;
144148
private int menuResId = -1;
145149
private boolean defaultScrollFlagsEnabled;
146150
private MaterialShapeDrawable backgroundShape;
151+
private boolean textCentered;
147152

148153
public SearchBar(@NonNull Context context) {
149154
this(context, null);
@@ -159,6 +164,9 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
159164
context = getContext();
160165
validateAttributes(attrs);
161166

167+
minimumTextMarginStart =
168+
getResources()
169+
.getDimensionPixelSize(R.dimen.m3_searchbar_text_margin_start_no_navigation_icon);
162170
defaultNavigationIcon =
163171
AppCompatResources.getDrawable(context, getDefaultNavigationIconResource());
164172
searchBarAnimationHelper = new SearchBarAnimationHelper();
@@ -185,6 +193,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
185193
String hint = a.getString(R.styleable.SearchBar_android_hint);
186194
float strokeWidth = a.getDimension(R.styleable.SearchBar_strokeWidth, -1);
187195
int strokeColor = a.getColor(R.styleable.SearchBar_strokeColor, Color.TRANSPARENT);
196+
textCentered = a.getBoolean(R.styleable.SearchBar_textCentered, false);
188197

189198
a.recycle();
190199

@@ -234,10 +243,7 @@ private void initTextView(@StyleRes int textAppearanceResId, String text, String
234243
}
235244
setText(text);
236245
setHint(hint);
237-
if (getNavigationIcon() == null) {
238-
((MarginLayoutParams) textView.getLayoutParams()).setMarginStart(getResources()
239-
.getDimensionPixelSize(R.dimen.m3_searchbar_text_margin_start_no_navigation_icon));
240-
}
246+
setTextCentered(textCentered);
241247
}
242248

243249
private void initBackground(
@@ -388,6 +394,36 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
388394

389395
layoutCenterView();
390396
setHandwritingBoundsInsets();
397+
// If after laying out, there's not enough space between the textview and the start of
398+
// the searchbar, we add a margin.
399+
if (textView != null) {
400+
Toolbar.LayoutParams lp = (LayoutParams) textView.getLayoutParams();
401+
int currentMargin = lp.getMarginStart();
402+
int newMargin = getNewMargin(currentMargin);
403+
if (currentMargin != newMargin) {
404+
lp.setMarginStart(newMargin);
405+
currentTextMarginStart = newMargin;
406+
textView.setLayoutParams(lp);
407+
}
408+
}
409+
}
410+
411+
private int getNewMargin(int currentMargin) {
412+
// The start position of the textview with respect to its parent, the searchbar.
413+
int textViewStart =
414+
getLayoutDirection() == LAYOUT_DIRECTION_RTL
415+
? getMeasuredWidth() - textView.getRight()
416+
: textView.getLeft();
417+
int additionalMarginStart = max(minimumTextMarginStart - textViewStart, 0);
418+
// If we are already including the margin in the textview start, and it is necessary to be added
419+
// (ie. keeping the desired distance between the textview and the start), we should keep
420+
// the margin.
421+
if (currentTextMarginStart != 0
422+
&& currentMargin == currentTextMarginStart
423+
&& (textViewStart - currentMargin) < minimumTextMarginStart) {
424+
additionalMarginStart = currentMargin;
425+
}
426+
return additionalMarginStart;
391427
}
392428

393429
@Override
@@ -564,6 +600,26 @@ public void setText(@StringRes int textResId) {
564600
textView.setText(textResId);
565601
}
566602

603+
/** Whether or not to center the text. */
604+
public void setTextCentered(boolean textCentered) {
605+
this.textCentered = textCentered;
606+
if (textView == null) {
607+
return;
608+
}
609+
Toolbar.LayoutParams lp = (LayoutParams) textView.getLayoutParams();
610+
if (textCentered) {
611+
lp.gravity = Gravity.CENTER_HORIZONTAL;
612+
} else {
613+
lp.gravity = Gravity.NO_GRAVITY;
614+
}
615+
textView.setLayoutParams(lp);
616+
}
617+
618+
/** Whether or not the text is centered. */
619+
public boolean getTextCentered() {
620+
return textCentered;
621+
}
622+
567623
/** Clears the text of main {@link TextView}. */
568624
public void clearText() {
569625
textView.setText("");

lib/java/com/google/android/material/search/res-public/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<public name="hideNavigationIcon" type="attr"/>
2828
<public name="forceDefaultNavigationOnClickListener" type="attr"/>
2929
<public name="tintNavigationIcon" type="attr"/>
30+
<public name="textCentered" type="attr"/>
3031
<public name="Widget.Material3.SearchBar" type="style"/>
3132
<public name="Widget.Material3.SearchBar.Outlined" type="style"/>
3233
<public name="Widget.Material3.SearchView" type="style"/>

lib/java/com/google/android/material/search/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
<attr name="strokeColor"/>
6565
<!-- Width of the SearchBar outline stroke. -->
6666
<attr name="strokeWidth"/>
67+
<!-- Whether the text corresponding to the `android:text` attribute or `android:hint` attribute
68+
should be centered horizontally within the searchbar. Default is false.-->
69+
<attr name="textCentered" format="boolean"/>
6770
</declare-styleable>
6871

6972
<declare-styleable name="SearchView">

0 commit comments

Comments
 (0)