Skip to content

Commit 3b333ee

Browse files
Material Design Teamhunterstich
authored andcommitted
[Buttons] Use Layout to measure text width.
This corrects icon placement when used with Spanned text. PiperOrigin-RevId: 483047554
1 parent e474cf4 commit 3b333ee

File tree

1 file changed

+4
-21
lines changed

1 file changed

+4
-21
lines changed

lib/java/com/google/android/material/button/MaterialButton.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
2222
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
23+
import static java.lang.Math.ceil;
2324
import static java.lang.Math.max;
2425
import static java.lang.Math.min;
2526

@@ -613,24 +614,12 @@ private void updateIconPosition(int buttonWidth, int buttonHeight) {
613614
}
614615

615616
private int getTextLayoutWidth() {
616-
int maxWidth = 0;
617+
float maxWidth = 0;
617618
int lineCount = getLineCount();
618619
for (int line = 0; line < lineCount; line++) {
619-
maxWidth = max(maxWidth, getTextWidth(getTextInLine(line)));
620+
maxWidth = max(maxWidth, getLayout().getLineWidth(line));
620621
}
621-
return maxWidth;
622-
}
623-
624-
private int getTextWidth(CharSequence text) {
625-
Paint textPaint = getPaint();
626-
String buttonText = text.toString();
627-
if (getTransformationMethod() != null) {
628-
// if text is transformed, add that transformation to to ensure correct calculation
629-
// of icon padding.
630-
buttonText = getTransformationMethod().getTransformation(buttonText, this).toString();
631-
}
632-
633-
return min((int) textPaint.measureText(buttonText), getLayout().getEllipsizedWidth());
622+
return (int) ceil(maxWidth);
634623
}
635624

636625
private int getTextHeight() {
@@ -652,12 +641,6 @@ private int getTextHeight() {
652641
return min(bounds.height(), getLayout().getHeight());
653642
}
654643

655-
private CharSequence getTextInLine(int line) {
656-
int start = getLayout().getLineStart(line);
657-
int end = getLayout().getLineEnd(line);
658-
return getText().subSequence(start, end);
659-
}
660-
661644
private boolean isLayoutRTL() {
662645
return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
663646
}

0 commit comments

Comments
 (0)