Skip to content

Commit 73cb1b6

Browse files
drchenafohrman
authored andcommittedMay 19, 2022
[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 #2573 PiperOrigin-RevId: 449597658
1 parent 673cefc commit 73cb1b6

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed
 

‎lib/java/com/google/android/material/internal/CollapsingTextHelper.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public final class CollapsingTextHelper {
9292

9393
private final View view;
9494

95-
private boolean drawTitle;
9695
private float expandedFraction;
9796
private boolean fadeModeEnabled;
9897
private float fadeModeStartFraction;
@@ -246,7 +245,6 @@ public void setExpandedBounds(int left, int top, int right, int bottom) {
246245
if (!rectEquals(expandedBounds, left, top, right, bottom)) {
247246
expandedBounds.set(left, top, right, bottom);
248247
boundsChanged = true;
249-
onBoundsChanged();
250248
}
251249
}
252250

@@ -258,7 +256,6 @@ public void setCollapsedBounds(int left, int top, int right, int bottom) {
258256
if (!rectEquals(collapsedBounds, left, top, right, bottom)) {
259257
collapsedBounds.set(left, top, right, bottom);
260258
boundsChanged = true;
261-
onBoundsChanged();
262259
}
263260
}
264261

@@ -268,9 +265,10 @@ public void setCollapsedBounds(@NonNull Rect bounds) {
268265

269266
public void getCollapsedTextActualBounds(@NonNull RectF bounds, int labelWidth, int textGravity) {
270267
isRtl = calculateIsRtl(text);
271-
bounds.left = getCollapsedTextLeftBound(labelWidth, textGravity);
268+
bounds.left = max(getCollapsedTextLeftBound(labelWidth, textGravity), collapsedBounds.left);
272269
bounds.top = collapsedBounds.top;
273-
bounds.right = getCollapsedTextRightBound(bounds, labelWidth, textGravity);
270+
bounds.right =
271+
min(getCollapsedTextRightBound(bounds, labelWidth, textGravity), collapsedBounds.right);
274272
bounds.bottom = collapsedBounds.top + getCollapsedTextHeight();
275273
}
276274

@@ -350,14 +348,6 @@ private void getTextPaintCollapsed(@NonNull TextPaint textPaint) {
350348
}
351349
}
352350

353-
void onBoundsChanged() {
354-
drawTitle =
355-
collapsedBounds.width() > 0
356-
&& collapsedBounds.height() > 0
357-
&& expandedBounds.width() > 0
358-
&& expandedBounds.height() > 0;
359-
}
360-
361351
public void setExpandedTextGravity(int gravity) {
362352
if (expandedTextGravity != gravity) {
363353
expandedTextGravity = gravity;
@@ -839,7 +829,7 @@ private void setExpandedTextBlend(float blend) {
839829
public void draw(@NonNull Canvas canvas) {
840830
final int saveCount = canvas.save();
841831
// Compute where to draw textLayout for this frame
842-
if (textToDraw != null && drawTitle) {
832+
if (textToDraw != null && currentBounds.width() > 0 && currentBounds.height() > 0) {
843833
textPaint.setTextSize(currentTextSize);
844834
float x = currentDrawX;
845835
float y = currentDrawY;

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

+3
Original file line numberDiff line numberDiff line change
@@ -3954,6 +3954,9 @@ private void openCutout() {
39543954
final RectF cutoutBounds = tmpRectF;
39553955
collapsingTextHelper.getCollapsedTextActualBounds(
39563956
cutoutBounds, editText.getWidth(), editText.getGravity());
3957+
if (cutoutBounds.width() <= 0 || cutoutBounds.height() <= 0) {
3958+
return;
3959+
}
39573960
applyCutoutPadding(cutoutBounds);
39583961

39593962
// Offset the cutout bounds by the TextInputLayout's paddings, half of the cutout height, and

0 commit comments

Comments
 (0)
Please sign in to comment.