Skip to content

Commit

Permalink
[TextField] Make collapsed hint aligned with prefix and suffix text
Browse files Browse the repository at this point in the history
The original logic prevents displaying the collapsed hint on top of suffix text. The behavior looks not consistent with the case of prefix text or outline variants of text fields.

This CL also fixes the issue that when drawable paddings are set on EditText, the collapsed hint is not aligned with the prefix anymore.

Resolves #2800

PiperOrigin-RevId: 511390069
  • Loading branch information
drchen authored and hunterstich committed Feb 23, 2023
1 parent 33e4f84 commit d9b97b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
Expand Up @@ -737,6 +737,21 @@ void updateSuffixTextViewPadding() {
textInputLayout.editText.getPaddingBottom());
}

int getSuffixTextEndOffset() {
int endIconOffset;
if (isEndIconVisible() || isErrorIconVisible()) {
endIconOffset =
endIconView.getMeasuredWidth()
+ MarginLayoutParamsCompat.getMarginStart(
(MarginLayoutParams) endIconView.getLayoutParams());
} else {
endIconOffset = 0;
}
return ViewCompat.getPaddingEnd(this)
+ ViewCompat.getPaddingEnd(suffixTextView)
+ endIconOffset;
}

@Nullable
CheckableImageButton getCurrentEndIconView() {
if (isErrorIconVisible()) {
Expand Down
Expand Up @@ -328,6 +328,21 @@ void updatePrefixTextViewPadding() {
editText.getCompoundPaddingBottom());
}

int getPrefixTextStartOffset() {
int startIconOffset;
if (isStartIconVisible()) {
startIconOffset =
startIconView.getMeasuredWidth()
+ MarginLayoutParamsCompat.getMarginEnd(
(MarginLayoutParams) startIconView.getLayoutParams());
} else {
startIconOffset = 0;
}
return ViewCompat.getPaddingStart(this)
+ ViewCompat.getPaddingStart(prefixTextView)
+ startIconOffset;
}

void onHintStateChanged(boolean hintExpanded) {
this.hintExpanded = hintExpanded;
updateVisibility();
Expand Down
34 changes: 18 additions & 16 deletions lib/java/com/google/android/material/textfield/TextInputLayout.java
Expand Up @@ -2769,35 +2769,37 @@ private Rect calculateCollapsedTextBounds(@NonNull Rect rect) {
bounds.right = rect.right - editText.getPaddingRight();
return bounds;
case BOX_BACKGROUND_FILLED:
bounds.left = getLabelLeftBoundAlightWithPrefix(rect.left, isRtl);
bounds.left = getLabelLeftBoundAlignedWithPrefixAndSuffix(rect.left, isRtl);
bounds.top = rect.top + boxCollapsedPaddingTopPx;
bounds.right = getLabelRightBoundAlignedWithSuffix(rect.right, isRtl);
bounds.right = getLabelRightBoundAlignedWithPrefixAndSuffix(rect.right, isRtl);
return bounds;
case BOX_BACKGROUND_NONE:
default:
bounds.left = getLabelLeftBoundAlightWithPrefix(rect.left, isRtl);
bounds.left = getLabelLeftBoundAlignedWithPrefixAndSuffix(rect.left, isRtl);
bounds.top = getPaddingTop();
bounds.right = getLabelRightBoundAlignedWithSuffix(rect.right, isRtl);
bounds.right = getLabelRightBoundAlignedWithPrefixAndSuffix(rect.right, isRtl);
return bounds;
}
}

private int getLabelLeftBoundAlightWithPrefix(int rectLeft, boolean isRtl) {
int left = rectLeft + editText.getCompoundPaddingLeft();
if (getPrefixText() != null && !isRtl) {
// Label should be vertically aligned with prefix
left = left - getPrefixTextView().getMeasuredWidth() + getPrefixTextView().getPaddingLeft();
private int getLabelLeftBoundAlignedWithPrefixAndSuffix(int rectLeft, boolean isRtl) {
if (!isRtl && getPrefixText() != null) {
return rectLeft + startLayout.getPrefixTextStartOffset();
}
return left;
if (isRtl && getSuffixText() != null) {
return rectLeft + endLayout.getSuffixTextEndOffset();
}
return rectLeft + editText.getCompoundPaddingLeft();
}

private int getLabelRightBoundAlignedWithSuffix(int rectRight, boolean isRtl) {
int right = rectRight - editText.getCompoundPaddingRight();
if (getPrefixText() != null && isRtl) {
// Label should be vertically aligned with prefix if in RTL
right += getPrefixTextView().getMeasuredWidth() - getPrefixTextView().getPaddingRight();
private int getLabelRightBoundAlignedWithPrefixAndSuffix(int rectRight, boolean isRtl) {
if (!isRtl && getSuffixText() != null) {
return rectRight - endLayout.getSuffixTextEndOffset();
}
if (isRtl && getPrefixText() != null) {
return rectRight - startLayout.getPrefixTextStartOffset();
}
return right;
return rectRight - editText.getCompoundPaddingRight();
}

@NonNull
Expand Down

0 comments on commit d9b97b4

Please sign in to comment.