Fix Toolbar title appears to lose bold
The issue here was not bold style, but sans-serif being used
instead of sans-serif-medium.
The code that handles setTextAppearance was not clearing the existing
value for the Typeface when a new value is present. Because of this,
if TypedArray#getFont threw an exception, the typeface remained in its
old value, and the following code relies on null as a signal that a
system font might need loading.
By resetting the value before re-calculating, the null
check works as expected.
Bug: 62449517
Test: AppCompatTextViewTest#testSetTextAppearance_resetTypeface
Change-Id: I4aaa7fb7f3d3a7f615a03fe6baeca7b5fce2d3d2
diff --git a/v7/appcompat/src/android/support/v7/widget/AppCompatTextHelper.java b/v7/appcompat/src/android/support/v7/widget/AppCompatTextHelper.java
index c5f6e17..10e508e 100644
--- a/v7/appcompat/src/android/support/v7/widget/AppCompatTextHelper.java
+++ b/v7/appcompat/src/android/support/v7/widget/AppCompatTextHelper.java
@@ -200,6 +200,7 @@
if (a.hasValue(R.styleable.TextAppearance_android_fontFamily)
|| a.hasValue(R.styleable.TextAppearance_fontFamily)) {
+ mFontTypeface = null;
int fontFamilyId = a.hasValue(R.styleable.TextAppearance_android_fontFamily)
? R.styleable.TextAppearance_android_fontFamily
: R.styleable.TextAppearance_fontFamily;
diff --git a/v7/appcompat/tests/res/values/strings.xml b/v7/appcompat/tests/res/values/strings.xml
index 8a1746b..86386e2 100644
--- a/v7/appcompat/tests/res/values/strings.xml
+++ b/v7/appcompat/tests/res/values/strings.xml
@@ -95,4 +95,5 @@
<string name="menu6">menu six</string>
<string name="font_sans_serif">sans-serif</string>
+ <string name="font_serif">serif</string>
</resources>
diff --git a/v7/appcompat/tests/res/values/styles.xml b/v7/appcompat/tests/res/values/styles.xml
index 81f8493..c08930a 100644
--- a/v7/appcompat/tests/res/values/styles.xml
+++ b/v7/appcompat/tests/res/values/styles.xml
@@ -70,4 +70,12 @@
<item name="fontFamily">@font/samplexmlfont</item>
<item name="android:textStyle">italic</item>
</style>
+
+ <style name="TextView_SansSerif">
+ <item name="android:fontFamily">@string/font_sans_serif</item>
+ </style>
+
+ <style name="TextView_Serif">
+ <item name="android:fontFamily">@string/font_serif</item>
+ </style>
</resources>
diff --git a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewTest.java b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewTest.java
index d1b00ad..5470329 100644
--- a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewTest.java
+++ b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewTest.java
@@ -24,6 +24,7 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
+import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
@@ -244,4 +245,17 @@
assertNotEquals(Typeface.DEFAULT, textView.getTypeface());
}
+
+ @Test
+ @UiThreadTest
+ public void testSetTextAppearance_resetTypeface() throws PackageManager.NameNotFoundException {
+ TextView textView = mContainer.findViewById(R.id.textview_simple);
+
+ TextViewCompat.setTextAppearance(textView, R.style.TextView_SansSerif);
+ Typeface firstTypeface = textView.getTypeface();
+
+ TextViewCompat.setTextAppearance(textView, R.style.TextView_Serif);
+ Typeface secondTypeface = textView.getTypeface();
+ assertNotEquals(firstTypeface, secondTypeface);
+ }
}