From 73cb1b6e78a0e28d157a1b5da061568117c2fa40 Mon Sep 17 00:00:00 2001 From: conradchen Date: Wed, 18 May 2022 18:52:33 -0400 Subject: [PATCH] [TextField] Fix hint is not displayed when expanding space is limited In CollapsingTextHelper we are checking both the expanded bound and the collapsed bound to be larger than 0 to decide if we are going to draw the collapsing text. However this can result in a situation that when there's no space to display expanded hint, the collapsed hint won't be drawn either, even if the hint is not expandable at all. This CL fixes the issue in a more generic way - whenever draw() is called, we check if the current bound (should be calculated whenever the collapsing fraction is changed) is larger than 0 to decide if we need to draw the collapsing text. This CL also adds the logic to ensure the cutout bound will never be larger than the collapsed bound. Resolves https://github.com/material-components/material-components-android/issues/2573 PiperOrigin-RevId: 449597658 --- .../internal/CollapsingTextHelper.java | 18 ++++-------------- .../material/textfield/TextInputLayout.java | 3 +++ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java index bea422678d8..a84e4d69dfd 100644 --- a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java +++ b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java @@ -92,7 +92,6 @@ public final class CollapsingTextHelper { private final View view; - private boolean drawTitle; private float expandedFraction; private boolean fadeModeEnabled; private float fadeModeStartFraction; @@ -246,7 +245,6 @@ public void setExpandedBounds(int left, int top, int right, int bottom) { if (!rectEquals(expandedBounds, left, top, right, bottom)) { expandedBounds.set(left, top, right, bottom); boundsChanged = true; - onBoundsChanged(); } } @@ -258,7 +256,6 @@ public void setCollapsedBounds(int left, int top, int right, int bottom) { if (!rectEquals(collapsedBounds, left, top, right, bottom)) { collapsedBounds.set(left, top, right, bottom); boundsChanged = true; - onBoundsChanged(); } } @@ -268,9 +265,10 @@ public void setCollapsedBounds(@NonNull Rect bounds) { public void getCollapsedTextActualBounds(@NonNull RectF bounds, int labelWidth, int textGravity) { isRtl = calculateIsRtl(text); - bounds.left = getCollapsedTextLeftBound(labelWidth, textGravity); + bounds.left = max(getCollapsedTextLeftBound(labelWidth, textGravity), collapsedBounds.left); bounds.top = collapsedBounds.top; - bounds.right = getCollapsedTextRightBound(bounds, labelWidth, textGravity); + bounds.right = + min(getCollapsedTextRightBound(bounds, labelWidth, textGravity), collapsedBounds.right); bounds.bottom = collapsedBounds.top + getCollapsedTextHeight(); } @@ -350,14 +348,6 @@ private void getTextPaintCollapsed(@NonNull TextPaint textPaint) { } } - void onBoundsChanged() { - drawTitle = - collapsedBounds.width() > 0 - && collapsedBounds.height() > 0 - && expandedBounds.width() > 0 - && expandedBounds.height() > 0; - } - public void setExpandedTextGravity(int gravity) { if (expandedTextGravity != gravity) { expandedTextGravity = gravity; @@ -839,7 +829,7 @@ private void setExpandedTextBlend(float blend) { public void draw(@NonNull Canvas canvas) { final int saveCount = canvas.save(); // Compute where to draw textLayout for this frame - if (textToDraw != null && drawTitle) { + if (textToDraw != null && currentBounds.width() > 0 && currentBounds.height() > 0) { textPaint.setTextSize(currentTextSize); float x = currentDrawX; float y = currentDrawY; diff --git a/lib/java/com/google/android/material/textfield/TextInputLayout.java b/lib/java/com/google/android/material/textfield/TextInputLayout.java index 7d70143b3e5..a30b55551a1 100644 --- a/lib/java/com/google/android/material/textfield/TextInputLayout.java +++ b/lib/java/com/google/android/material/textfield/TextInputLayout.java @@ -3954,6 +3954,9 @@ private void openCutout() { final RectF cutoutBounds = tmpRectF; collapsingTextHelper.getCollapsedTextActualBounds( cutoutBounds, editText.getWidth(), editText.getGravity()); + if (cutoutBounds.width() <= 0 || cutoutBounds.height() <= 0) { + return; + } applyCutoutPadding(cutoutBounds); // Offset the cutout bounds by the TextInputLayout's paddings, half of the cutout height, and