From 2590c42764fc2178114bcd53c339b5368ba2c5d3 Mon Sep 17 00:00:00 2001 From: pubiqq Date: Thu, 19 Oct 2023 05:32:29 -0700 Subject: [PATCH] [TextInputLayout] Fix editText paddings on pre-Lollipop Resolves https://github.com/material-components/material-components-android/pull/3583 Resolves https://github.com/material-components/material-components-android/issues/3582 GIT_ORIGIN_REV_ID=356f7488505470656d26b0452dae78ae01475022 PiperOrigin-RevId: 574833205 --- .../material/textfield/TextInputLayout.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/java/com/google/android/material/textfield/TextInputLayout.java b/lib/java/com/google/android/material/textfield/TextInputLayout.java index 090ef96f850..514fe971552 100644 --- a/lib/java/com/google/android/material/textfield/TextInputLayout.java +++ b/lib/java/com/google/android/material/textfield/TextInputLayout.java @@ -846,10 +846,30 @@ void updateEditTextBoxBackgroundIfNeeded() { || boxBackgroundMode == BOX_BACKGROUND_NONE) { return; } - ViewCompat.setBackground(editText, getEditTextBoxBackground()); + updateEditTextBoxBackground(); boxBackgroundApplied = true; } + private void updateEditTextBoxBackground() { + Drawable editTextBoxBackground = getEditTextBoxBackground(); + + // On pre-Lollipop, setting a LayerDrawable as a background always replaces the original view + // paddings with its own, so we preserve the original paddings and restore them after setting + // a new background. + if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP + && editTextBoxBackground instanceof LayerDrawable) { + int paddingLeft = editText.getPaddingLeft(); + int paddingTop = editText.getPaddingTop(); + int paddingRight = editText.getPaddingRight(); + int paddingBottom = editText.getPaddingBottom(); + + ViewCompat.setBackground(editText, editTextBoxBackground); + editText.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); + } else { + ViewCompat.setBackground(editText, editTextBoxBackground); + } + } + @Nullable private Drawable getEditTextBoxBackground() { if (!(editText instanceof AutoCompleteTextView) || isEditable(editText)) {