Skip to content

Commit eabbfa2

Browse files
leticiarossiwcshi
authored andcommitted
Limiting helper/error view width so that counter view doesn't get pushed out.
Resolves: #689 PiperOrigin-RevId: 284594896
1 parent 8cb00cb commit eabbfa2

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

lib/java/com/google/android/material/textfield/IndicatorViewController.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.content.Context;
2929
import android.content.res.ColorStateList;
3030
import android.graphics.Typeface;
31+
import android.os.Build.VERSION;
3132
import androidx.annotation.ColorInt;
3233
import androidx.annotation.IntDef;
3334
import androidx.annotation.NonNull;
@@ -42,7 +43,6 @@
4243
import android.view.ViewGroup.LayoutParams;
4344
import android.widget.FrameLayout;
4445
import android.widget.LinearLayout;
45-
import android.widget.Space;
4646
import android.widget.TextView;
4747
import com.google.android.material.animation.AnimationUtils;
4848
import com.google.android.material.animation.AnimatorSetCompat;
@@ -53,7 +53,8 @@
5353

5454
/**
5555
* Controller for indicator views underneath the text input line in {@link
56-
* com.google.android.material.textfield.TextInputLayout}. This class controls helper and error views.
56+
* com.google.android.material.textfield.TextInputLayout}. This class controls helper and error
57+
* views.
5758
*/
5859
final class IndicatorViewController {
5960

@@ -371,14 +372,9 @@ void addIndicator(TextView indicator, @IndicatorIndex int index) {
371372
textInputView.addView(indicatorArea, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
372373

373374
captionArea = new FrameLayout(context);
374-
indicatorArea.addView(
375-
captionArea,
376-
-1,
377-
new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
378-
379-
final Space spacer = new Space(context);
380-
final LayoutParams spacerLp = new LinearLayout.LayoutParams(0, 0, 1f);
381-
indicatorArea.addView(spacer, spacerLp);
375+
LinearLayout.LayoutParams captionAreaLp =
376+
new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
377+
indicatorArea.addView(captionArea, captionAreaLp);
382378

383379
if (textInputView.getEditText() != null) {
384380
adjustIndicatorPadding();
@@ -390,7 +386,9 @@ void addIndicator(TextView indicator, @IndicatorIndex int index) {
390386
captionArea.addView(indicator);
391387
captionViewsAdded++;
392388
} else {
393-
indicatorArea.addView(indicator, index);
389+
LinearLayout.LayoutParams indicatorAreaLp =
390+
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
391+
indicatorArea.addView(indicator, indicatorAreaLp);
394392
}
395393
indicatorArea.setVisibility(VISIBLE);
396394
indicatorsAdded++;
@@ -430,6 +428,9 @@ void setErrorEnabled(boolean enabled) {
430428
if (enabled) {
431429
errorView = new AppCompatTextView(context);
432430
errorView.setId(R.id.textinput_error);
431+
if (VERSION.SDK_INT >= 17) {
432+
errorView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
433+
}
433434
if (typeface != null) {
434435
errorView.setTypeface(typeface);
435436
}
@@ -469,6 +470,9 @@ void setHelperTextEnabled(boolean enabled) {
469470
if (enabled) {
470471
helperTextView = new AppCompatTextView(context);
471472
helperTextView.setId(R.id.textinput_helper_text);
473+
if (VERSION.SDK_INT >= 17) {
474+
helperTextView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
475+
}
472476
if (typeface != null) {
473477
helperTextView.setTypeface(typeface);
474478
}

lib/java/com/google/android/material/textfield/TextInputLayout.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,9 @@ public void setCounterEnabled(boolean enabled) {
17291729
}
17301730
counterView.setMaxLines(1);
17311731
indicatorViewController.addIndicator(counterView, COUNTER_INDEX);
1732+
MarginLayoutParamsCompat.setMarginStart(
1733+
(MarginLayoutParams) counterView.getLayoutParams(),
1734+
getResources().getDimensionPixelOffset(R.dimen.mtrl_textinput_counter_margin_start));
17321735
updateCounterTextAppearanceAndColor();
17331736
updateCounter();
17341737
} else {

lib/java/com/google/android/material/textfield/res/values/dimens.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
<dimen name="design_textinput_caption_translate_y">5dp</dimen>
2020

21+
<dimen name="mtrl_textinput_counter_margin_start">16dp</dimen>
22+
2123
<dimen name="mtrl_textinput_outline_box_expanded_padding">16dp</dimen>
2224

2325
<dimen name="mtrl_textinput_box_corner_radius_small">0dp</dimen>

0 commit comments

Comments
 (0)