Skip to content

Commit 44d4c3e

Browse files
committed
[TopAppBar] Correctly set letter spacing when expanding titles
We recreated title text layout when expanding from the collapsed state but didn't correctly set the letter spacing to the expanded one. The letter spacing being used will be a interpolated value between collapsed and expanded letter spacing (close to the collapsed one due to the implementation). This will cause the text layout incorrectly calculate the line count from the wrong letter spacing and result in a broken layout. Correctly sets the letter spacing to fix the issue. Resolves #2463 PiperOrigin-RevId: 408910555
1 parent 34cce18 commit 44d4c3e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public final class CollapsingTextHelper {
153153

154154
private float collapsedLetterSpacing;
155155
private float expandedLetterSpacing;
156+
private float currentLetterSpacing;
156157

157158
private StaticLayout textLayout;
158159
private float collapsedTextWidth;
@@ -907,10 +908,12 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
907908

908909
float availableWidth;
909910
float newTextSize;
911+
float newLetterSpacing;
910912
boolean updateDrawText = false;
911913

912914
if (isClose(textSize, collapsedTextSize)) {
913915
newTextSize = collapsedTextSize;
916+
newLetterSpacing = collapsedLetterSpacing;
914917
scale = 1f;
915918
if (currentTypeface != collapsedTypeface) {
916919
currentTypeface = collapsedTypeface;
@@ -919,6 +922,7 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
919922
availableWidth = collapsedWidth;
920923
} else {
921924
newTextSize = expandedTextSize;
925+
newLetterSpacing = expandedLetterSpacing;
922926
if (currentTypeface != expandedTypeface) {
923927
currentTypeface = expandedTypeface;
924928
updateDrawText = true;
@@ -953,14 +957,20 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
953957
}
954958

955959
if (availableWidth > 0) {
956-
updateDrawText = (currentTextSize != newTextSize) || boundsChanged || updateDrawText;
960+
boolean textSizeChanged = currentTextSize != newTextSize;
961+
boolean letterSpacingChanged = currentLetterSpacing != newLetterSpacing;
962+
updateDrawText = textSizeChanged || letterSpacingChanged || boundsChanged || updateDrawText;
957963
currentTextSize = newTextSize;
964+
currentLetterSpacing = newLetterSpacing;
958965
boundsChanged = false;
959966
}
960967

961968
if (textToDraw == null || updateDrawText) {
962969
textPaint.setTextSize(currentTextSize);
963970
textPaint.setTypeface(currentTypeface);
971+
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
972+
textPaint.setLetterSpacing(currentLetterSpacing);
973+
}
964974
// Use linear text scaling if we're scaling the canvas
965975
textPaint.setLinearText(scale != 1f);
966976

0 commit comments

Comments
 (0)