Skip to content

Commit

Permalink
[CollapsingToolbarLayout] Fixed RTL text only laying out as RTL when …
Browse files Browse the repository at this point in the history
…actual text is RTL

PiperOrigin-RevId: 374663684
(cherry picked from commit 841f229)
  • Loading branch information
dsn5ft committed Jun 2, 2021
1 parent a096515 commit 72b0c39
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet

collapsingTextHelper = new CollapsingTextHelper(this);
collapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);
collapsingTextHelper.setRtlTextDirectionHeuristicsEnabled(false);

elevationOverlayProvider = new ElevationOverlayProvider(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public final class CollapsingTextHelper {
@Nullable private CharSequence text;
@Nullable private CharSequence textToDraw;
private boolean isRtl;
private boolean isRtlTextDirectionHeuristicsEnabled = true;

private boolean useTexture;
@Nullable private Bitmap expandedTitleTexture;
Expand Down Expand Up @@ -522,6 +523,14 @@ public float getExpandedTextSize() {
return expandedTextSize;
}

public void setRtlTextDirectionHeuristicsEnabled(boolean rtlTextDirectionHeuristicsEnabled) {
isRtlTextDirectionHeuristicsEnabled = rtlTextDirectionHeuristicsEnabled;
}

public boolean isRtlTextDirectionHeuristicsEnabled() {
return isRtlTextDirectionHeuristicsEnabled;
}

private void calculateCurrentOffsets() {
calculateOffsets(expandedFraction);
}
Expand Down Expand Up @@ -840,16 +849,22 @@ private void drawMultilineTransition(@NonNull Canvas canvas, float currentExpand

private boolean calculateIsRtl(@NonNull CharSequence text) {
final boolean defaultIsRtl = isDefaultIsRtl();
return (defaultIsRtl
? TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL
: TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR)
.isRtl(text, 0, text.length());
return isRtlTextDirectionHeuristicsEnabled
? isTextDirectionHeuristicsIsRtl(text, defaultIsRtl)
: defaultIsRtl;
}

private boolean isDefaultIsRtl() {
return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
}

private boolean isTextDirectionHeuristicsIsRtl(@NonNull CharSequence text, boolean defaultIsRtl) {
return (defaultIsRtl
? TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL
: TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR)
.isRtl(text, 0, text.length());
}

private void setInterpolatedTextSize(float textSize) {
calculateUsingTextSize(textSize);

Expand Down

0 comments on commit 72b0c39

Please sign in to comment.