Skip to content

Commit

Permalink
Fixing input text not adjusting to start/end icons in some cases.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 256369941
  • Loading branch information
leticiarossi authored and raajkumars committed Jul 3, 2019
1 parent 4213964 commit 15b721c
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions lib/java/com/google/android/material/textfield/TextInputLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -1995,12 +1995,17 @@ public void setHintAnimationEnabled(boolean enabled) {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setEditTextHeightAndDummyDrawables();

boolean updatedHeight = updateEditTextHeightBasedOnIcon();
boolean updatedIcon = updateIconDummyDrawables();
if (updatedHeight || updatedIcon) {
editText.post(() -> editText.requestLayout());
}
}

private void setEditTextHeightAndDummyDrawables() {
private boolean updateEditTextHeightBasedOnIcon() {
if (editText == null) {
return;
return false;
}

// We need to make sure that the EditText's height is at least the same as the end or start
Expand All @@ -2010,10 +2015,10 @@ private void setEditTextHeightAndDummyDrawables() {
Math.max(endIconView.getMeasuredHeight(), startIconView.getMeasuredHeight());
if (editText.getMeasuredHeight() < maxIconHeight) {
editText.setMinimumHeight(maxIconHeight);
editText.post(() -> editText.requestLayout());
return true;
}

updateIconDummyDrawables();
return false;
}

/**
Expand Down Expand Up @@ -2667,33 +2672,40 @@ private void applyEndIconTint() {
* We need to add a dummy drawable as the start and/or end compound drawables so that the text is
* indented and doesn't display below the icon views.
*/
private void updateIconDummyDrawables() {
private boolean updateIconDummyDrawables() {
if (editText == null) {
return;
return false;
}

boolean updatedIcon = false;
// Update start icon drawable if needed.
if (hasStartIcon() && isStartIconVisible()) {
startIconDummyDrawable = new ColorDrawable();
int right =
startIconView.getMeasuredWidth()
- editText.getPaddingLeft()
+ MarginLayoutParamsCompat.getMarginEnd(
((MarginLayoutParams) startIconView.getLayoutParams()));
startIconDummyDrawable.setBounds(0, 0, right, 1);
if (hasStartIcon() && isStartIconVisible() && startIconView.getMeasuredWidth() > 0) {
if (startIconDummyDrawable == null) {
startIconDummyDrawable = new ColorDrawable();
int right =
startIconView.getMeasuredWidth()
- editText.getPaddingLeft()
+ MarginLayoutParamsCompat.getMarginEnd(
((MarginLayoutParams) startIconView.getLayoutParams()));
startIconDummyDrawable.setBounds(0, 0, right, 1);
}
final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(editText);
TextViewCompat.setCompoundDrawablesRelative(
editText, startIconDummyDrawable, compounds[1], compounds[2], compounds[3]);
if (compounds[0] != startIconDummyDrawable) {
TextViewCompat.setCompoundDrawablesRelative(
editText, startIconDummyDrawable, compounds[1], compounds[2], compounds[3]);
updatedIcon = true;
}
} else if (startIconDummyDrawable != null) {
// Remove the dummy start compound drawable if it exists and clear it.
final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(editText);
TextViewCompat.setCompoundDrawablesRelative(
editText, null, compounds[1], compounds[2], compounds[3]);
startIconDummyDrawable = null;
updatedIcon = true;
}

// Update end icon drawable if needed.
if (hasEndIcon() && isEndIconVisible()) {
if (hasEndIcon() && isEndIconVisible() && endIconView.getMeasuredWidth() > 0) {
if (endIconDummyDrawable == null) {
endIconDummyDrawable = new ColorDrawable();
int right =
Expand All @@ -2709,16 +2721,20 @@ private void updateIconDummyDrawables() {
originalEditTextEndDrawable = compounds[2];
TextViewCompat.setCompoundDrawablesRelative(
editText, compounds[0], compounds[1], endIconDummyDrawable, compounds[3]);
updatedIcon = true;
}
} else if (endIconDummyDrawable != null) {
// Remove the dummy end compound drawable if it exists and clear it.
final Drawable[] compounds = TextViewCompat.getCompoundDrawablesRelative(editText);
if (compounds[2] == endIconDummyDrawable) {
TextViewCompat.setCompoundDrawablesRelative(
editText, compounds[0], compounds[1], originalEditTextEndDrawable, compounds[3]);
updatedIcon = true;
}
endIconDummyDrawable = null;
}

return updatedIcon;
}

private void applyIconTint(
Expand Down

0 comments on commit 15b721c

Please sign in to comment.