From 5dce006a5c1649df4eb6631c85619017728726af Mon Sep 17 00:00:00 2001 From: Material Design Team Date: Mon, 31 Jan 2022 12:50:46 -0800 Subject: [PATCH] [CollapsingToolbarLayout] Constrain adjusted font weight to acceptable values PiperOrigin-RevId: 425438646 --- .../material/internal/CollapsingTextHelper.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java index 7a677943cb7..3d551381fab 100644 --- a/lib/java/com/google/android/material/internal/CollapsingTextHelper.java +++ b/lib/java/com/google/android/material/internal/CollapsingTextHelper.java @@ -523,16 +523,20 @@ public void maybeUpdateFontWeightAdjustment(@NonNull Configuration configuration } private boolean shouldUseBoldTypefaces() { - return VERSION.SDK_INT >= VERSION_CODES.S && fontWeightAdjustment >= FontStyle.FONT_WEIGHT_MIN; + return VERSION.SDK_INT >= VERSION_CODES.S + && fontWeightAdjustment != Configuration.FONT_WEIGHT_ADJUSTMENT_UNDEFINED + && fontWeightAdjustment != 0; } @Nullable private Typeface maybeCloneWithAdjustment(@NonNull Typeface typeface) { if (shouldUseBoldTypefaces()) { - return Typeface.create( - typeface, - typeface.getWeight() + fontWeightAdjustment, - typeface.isItalic()); + int adjustedWeight = + MathUtils.clamp( + typeface.getWeight() + fontWeightAdjustment, + FontStyle.FONT_WEIGHT_MIN, + FontStyle.FONT_WEIGHT_MAX); + return Typeface.create(typeface, adjustedWeight, typeface.isItalic()); } return null; }