Skip to content

Commit fb8b58d

Browse files
committed
[TextInputLayout] Update text paddings in compound layouts uniformly
1 parent 39be13c commit fb8b58d

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ private void initSuffixTextView(TintTypedArray a) {
306306
setSuffixText(a.getText(R.styleable.TextInputLayout_suffixText));
307307
}
308308

309+
@Override
310+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
311+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
312+
updateSuffixTextViewPadding();
313+
}
314+
309315
void setErrorIconDrawable(@DrawableRes int resId) {
310316
setErrorIconDrawable(resId != 0 ? AppCompatResources.getDrawable(getContext(), resId) : null);
311317
refreshErrorIconDrawableState();
@@ -717,18 +723,19 @@ private void updateSuffixTextVisibility() {
717723
}
718724

719725
void updateSuffixTextViewPadding() {
720-
if (textInputLayout.editText == null) {
726+
EditText editText = textInputLayout.editText;
727+
if (editText == null) {
721728
return;
722729
}
723730
int endPadding =
724-
(isEndIconVisible() || isErrorIconVisible()) ? 0 : textInputLayout.editText.getPaddingEnd();
731+
(isEndIconVisible() || isErrorIconVisible()) ? 0 : editText.getPaddingEnd();
725732
suffixTextView.setPaddingRelative(
726733
getContext()
727734
.getResources()
728735
.getDimensionPixelSize(R.dimen.material_input_text_to_prefix_suffix_padding),
729-
textInputLayout.editText.getPaddingTop(),
736+
editText.getCompoundPaddingTop(),
730737
endPadding,
731-
textInputLayout.editText.getPaddingBottom());
738+
editText.getCompoundPaddingBottom());
732739
}
733740

734741
int getSuffixTextEndOffset() {

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
* developer guidance</a> and <a href="https://material.io/components/text-fields/overview">design
202202
* guidelines</a>.
203203
*/
204-
public class TextInputLayout extends LinearLayout implements OnGlobalLayoutListener {
204+
public class TextInputLayout extends LinearLayout {
205205

206206
private static final String TAG = "TextInputLayout";
207207

@@ -461,8 +461,6 @@ public interface OnEndIconChangedListener {
461461

462462
private boolean restoringSavedState;
463463

464-
private boolean globalLayoutListenerAdded = false;
465-
466464
public TextInputLayout(@NonNull Context context) {
467465
this(context, null);
468466
}
@@ -729,17 +727,6 @@ public TextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs, i
729727
setHelperText(helperText);
730728
}
731729

732-
@Override
733-
public void onGlobalLayout() {
734-
endLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
735-
globalLayoutListenerAdded = false;
736-
boolean updatedHeight = updateEditTextHeightBasedOnIcon();
737-
boolean updatedIcon = updateDummyDrawables();
738-
if (updatedHeight || updatedIcon) {
739-
editText.post(() -> editText.requestLayout());
740-
}
741-
}
742-
743730
@Override
744731
public void addView(
745732
@NonNull View child, int index, @NonNull final ViewGroup.LayoutParams params) {
@@ -1605,7 +1592,6 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {}
16051592
startLayout.bringToFront();
16061593
endLayout.bringToFront();
16071594
dispatchOnEditTextAttached();
1608-
endLayout.updateSuffixTextViewPadding();
16091595

16101596
// Only call setEnabled on the edit text if the layout is disabled, to prevent reenabling an
16111597
// already disabled edit text.
@@ -3308,12 +3294,13 @@ public void onRtlPropertiesChanged(int layoutDirection) {
33083294
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
33093295
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
33103296

3311-
if (!globalLayoutListenerAdded) {
3312-
endLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
3313-
globalLayoutListenerAdded = true;
3297+
boolean updatedHeight = updateEditTextHeightBasedOnIcon();
3298+
boolean updatedIcon = updateDummyDrawables();
3299+
if (updatedHeight || updatedIcon) {
3300+
editText.post(() -> editText.requestLayout());
33143301
}
3302+
33153303
updatePlaceholderMeasurementsBasedOnEditText();
3316-
endLayout.updateSuffixTextViewPadding();
33173304

33183305
if (!isHintTextSingleLine()) {
33193306
updateCollapsingTextDimens(

0 commit comments

Comments
 (0)