20
20
21
21
import static androidx .annotation .RestrictTo .Scope .LIBRARY_GROUP ;
22
22
import static com .google .android .material .theme .overlay .MaterialThemeOverlay .wrap ;
23
+ import static java .lang .Math .max ;
23
24
24
25
import android .animation .AnimatorListenerAdapter ;
25
26
import android .content .Context ;
40
41
import androidx .appcompat .widget .Toolbar ;
41
42
import android .text .TextUtils ;
42
43
import android .util .AttributeSet ;
44
+ import android .view .Gravity ;
43
45
import android .view .LayoutInflater ;
44
46
import android .view .Menu ;
45
47
import android .view .View ;
@@ -138,12 +140,15 @@ public class SearchBar extends Toolbar {
138
140
private final Drawable defaultNavigationIcon ;
139
141
private final boolean tintNavigationIcon ;
140
142
private final boolean forceDefaultNavigationOnClickListener ;
143
+ private final int minimumTextMarginStart ;
144
+ private int currentTextMarginStart ;
141
145
@ Nullable private View centerView ;
142
146
@ Nullable private Integer navigationIconTint ;
143
147
@ Nullable private Drawable originalNavigationIconBackground ;
144
148
private int menuResId = -1 ;
145
149
private boolean defaultScrollFlagsEnabled ;
146
150
private MaterialShapeDrawable backgroundShape ;
151
+ private boolean textCentered ;
147
152
148
153
public SearchBar (@ NonNull Context context ) {
149
154
this (context , null );
@@ -159,6 +164,9 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
159
164
context = getContext ();
160
165
validateAttributes (attrs );
161
166
167
+ minimumTextMarginStart =
168
+ getResources ()
169
+ .getDimensionPixelSize (R .dimen .m3_searchbar_text_margin_start_no_navigation_icon );
162
170
defaultNavigationIcon =
163
171
AppCompatResources .getDrawable (context , getDefaultNavigationIconResource ());
164
172
searchBarAnimationHelper = new SearchBarAnimationHelper ();
@@ -185,6 +193,7 @@ public SearchBar(@NonNull Context context, @Nullable AttributeSet attrs, int def
185
193
String hint = a .getString (R .styleable .SearchBar_android_hint );
186
194
float strokeWidth = a .getDimension (R .styleable .SearchBar_strokeWidth , -1 );
187
195
int strokeColor = a .getColor (R .styleable .SearchBar_strokeColor , Color .TRANSPARENT );
196
+ textCentered = a .getBoolean (R .styleable .SearchBar_textCentered , false );
188
197
189
198
a .recycle ();
190
199
@@ -234,10 +243,7 @@ private void initTextView(@StyleRes int textAppearanceResId, String text, String
234
243
}
235
244
setText (text );
236
245
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 );
241
247
}
242
248
243
249
private void initBackground (
@@ -388,6 +394,36 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
388
394
389
395
layoutCenterView ();
390
396
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 ;
391
427
}
392
428
393
429
@ Override
@@ -564,6 +600,26 @@ public void setText(@StringRes int textResId) {
564
600
textView .setText (textResId );
565
601
}
566
602
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
+
567
623
/** Clears the text of main {@link TextView}. */
568
624
public void clearText () {
569
625
textView .setText ("" );
0 commit comments