Skip to content

Commit d9b97b4

Browse files
drchenhunterstich
authored andcommitted
[TextField] Make collapsed hint aligned with prefix and suffix text
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
1 parent 33e4f84 commit d9b97b4

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

lib/java/com/google/android/material/textfield/EndCompoundLayout.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,21 @@ void updateSuffixTextViewPadding() {
737737
textInputLayout.editText.getPaddingBottom());
738738
}
739739

740+
int getSuffixTextEndOffset() {
741+
int endIconOffset;
742+
if (isEndIconVisible() || isErrorIconVisible()) {
743+
endIconOffset =
744+
endIconView.getMeasuredWidth()
745+
+ MarginLayoutParamsCompat.getMarginStart(
746+
(MarginLayoutParams) endIconView.getLayoutParams());
747+
} else {
748+
endIconOffset = 0;
749+
}
750+
return ViewCompat.getPaddingEnd(this)
751+
+ ViewCompat.getPaddingEnd(suffixTextView)
752+
+ endIconOffset;
753+
}
754+
740755
@Nullable
741756
CheckableImageButton getCurrentEndIconView() {
742757
if (isErrorIconVisible()) {

lib/java/com/google/android/material/textfield/StartCompoundLayout.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,21 @@ void updatePrefixTextViewPadding() {
328328
editText.getCompoundPaddingBottom());
329329
}
330330

331+
int getPrefixTextStartOffset() {
332+
int startIconOffset;
333+
if (isStartIconVisible()) {
334+
startIconOffset =
335+
startIconView.getMeasuredWidth()
336+
+ MarginLayoutParamsCompat.getMarginEnd(
337+
(MarginLayoutParams) startIconView.getLayoutParams());
338+
} else {
339+
startIconOffset = 0;
340+
}
341+
return ViewCompat.getPaddingStart(this)
342+
+ ViewCompat.getPaddingStart(prefixTextView)
343+
+ startIconOffset;
344+
}
345+
331346
void onHintStateChanged(boolean hintExpanded) {
332347
this.hintExpanded = hintExpanded;
333348
updateVisibility();

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,35 +2769,37 @@ private Rect calculateCollapsedTextBounds(@NonNull Rect rect) {
27692769
bounds.right = rect.right - editText.getPaddingRight();
27702770
return bounds;
27712771
case BOX_BACKGROUND_FILLED:
2772-
bounds.left = getLabelLeftBoundAlightWithPrefix(rect.left, isRtl);
2772+
bounds.left = getLabelLeftBoundAlignedWithPrefixAndSuffix(rect.left, isRtl);
27732773
bounds.top = rect.top + boxCollapsedPaddingTopPx;
2774-
bounds.right = getLabelRightBoundAlignedWithSuffix(rect.right, isRtl);
2774+
bounds.right = getLabelRightBoundAlignedWithPrefixAndSuffix(rect.right, isRtl);
27752775
return bounds;
27762776
case BOX_BACKGROUND_NONE:
27772777
default:
2778-
bounds.left = getLabelLeftBoundAlightWithPrefix(rect.left, isRtl);
2778+
bounds.left = getLabelLeftBoundAlignedWithPrefixAndSuffix(rect.left, isRtl);
27792779
bounds.top = getPaddingTop();
2780-
bounds.right = getLabelRightBoundAlignedWithSuffix(rect.right, isRtl);
2780+
bounds.right = getLabelRightBoundAlignedWithPrefixAndSuffix(rect.right, isRtl);
27812781
return bounds;
27822782
}
27832783
}
27842784

2785-
private int getLabelLeftBoundAlightWithPrefix(int rectLeft, boolean isRtl) {
2786-
int left = rectLeft + editText.getCompoundPaddingLeft();
2787-
if (getPrefixText() != null && !isRtl) {
2788-
// Label should be vertically aligned with prefix
2789-
left = left - getPrefixTextView().getMeasuredWidth() + getPrefixTextView().getPaddingLeft();
2785+
private int getLabelLeftBoundAlignedWithPrefixAndSuffix(int rectLeft, boolean isRtl) {
2786+
if (!isRtl && getPrefixText() != null) {
2787+
return rectLeft + startLayout.getPrefixTextStartOffset();
27902788
}
2791-
return left;
2789+
if (isRtl && getSuffixText() != null) {
2790+
return rectLeft + endLayout.getSuffixTextEndOffset();
2791+
}
2792+
return rectLeft + editText.getCompoundPaddingLeft();
27922793
}
27932794

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

28032805
@NonNull

0 commit comments

Comments
 (0)