Skip to content

Commit 72b0c39

Browse files
committed
[CollapsingToolbarLayout] Fixed RTL text only laying out as RTL when actual text is RTL
PiperOrigin-RevId: 374663684 (cherry picked from commit 841f229)
1 parent a096515 commit 72b0c39

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet
189189

190190
collapsingTextHelper = new CollapsingTextHelper(this);
191191
collapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);
192+
collapsingTextHelper.setRtlTextDirectionHeuristicsEnabled(false);
192193

193194
elevationOverlayProvider = new ElevationOverlayProvider(context);
194195

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public final class CollapsingTextHelper {
117117
@Nullable private CharSequence text;
118118
@Nullable private CharSequence textToDraw;
119119
private boolean isRtl;
120+
private boolean isRtlTextDirectionHeuristicsEnabled = true;
120121

121122
private boolean useTexture;
122123
@Nullable private Bitmap expandedTitleTexture;
@@ -522,6 +523,14 @@ public float getExpandedTextSize() {
522523
return expandedTextSize;
523524
}
524525

526+
public void setRtlTextDirectionHeuristicsEnabled(boolean rtlTextDirectionHeuristicsEnabled) {
527+
isRtlTextDirectionHeuristicsEnabled = rtlTextDirectionHeuristicsEnabled;
528+
}
529+
530+
public boolean isRtlTextDirectionHeuristicsEnabled() {
531+
return isRtlTextDirectionHeuristicsEnabled;
532+
}
533+
525534
private void calculateCurrentOffsets() {
526535
calculateOffsets(expandedFraction);
527536
}
@@ -840,16 +849,22 @@ private void drawMultilineTransition(@NonNull Canvas canvas, float currentExpand
840849

841850
private boolean calculateIsRtl(@NonNull CharSequence text) {
842851
final boolean defaultIsRtl = isDefaultIsRtl();
843-
return (defaultIsRtl
844-
? TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL
845-
: TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR)
846-
.isRtl(text, 0, text.length());
852+
return isRtlTextDirectionHeuristicsEnabled
853+
? isTextDirectionHeuristicsIsRtl(text, defaultIsRtl)
854+
: defaultIsRtl;
847855
}
848856

849857
private boolean isDefaultIsRtl() {
850858
return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
851859
}
852860

861+
private boolean isTextDirectionHeuristicsIsRtl(@NonNull CharSequence text, boolean defaultIsRtl) {
862+
return (defaultIsRtl
863+
? TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL
864+
: TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR)
865+
.isRtl(text, 0, text.length());
866+
}
867+
853868
private void setInterpolatedTextSize(float textSize) {
854869
calculateUsingTextSize(textSize);
855870

0 commit comments

Comments
 (0)