3

I am updating font in my application by using the support library. Font is not getting updated on AppCompatCheckBox at runtime but in layout preview it is working fine. There are two styles.xml files in my application and I applied AppBaseTheme to all my activities.

default styles.xml

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorPrimary">@color/material_blue_500</item>

    <!-- Android widgets styles overridden -->

    <item name="android:checkboxStyle">@style/AppCheckBoxStyle</item>
    <item name="checkboxStyle">@style/AppCheckBoxStyle</item>

    <item name="android:radioButtonStyle">@style/AppRadioButtonStyle</item>
    <item name="radioButtonStyle">@style/AppRadioButtonStyle</item>


</style>

<style name="AppCheckBoxStyle" parent="android:Widget.CompoundButton.CheckBox">
    <item name="android:fontFamily">@font/allura_regular</item>
</style>

<style name="AppRadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton">
    <item name="android:fontFamily">@font/allura_regular</item>
</style>

v21/styles.xml

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorPrimary">@color/material_blue_500</item>

    <!-- Android widgets styles overridden -->

    <item name="android:checkboxStyle">@style/AppCheckBoxStyle</item>
    <item name="checkboxStyle">@style/AppCheckBoxStyle</item>

    <item name="android:radioButtonStyle">@style/AppRadioButtonStyle</item>
    <item name="radioButtonStyle">@style/AppRadioButtonStyle</item>


</style>

 <style name="AppCheckBoxStyle" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="android:fontFamily">@font/allura_regular</item>
</style>

<style name="AppRadioButtonStyle" parent="android:Widget.CompoundButton.RadioButton">
    <item name="android:fontFamily">@font/allura_regular</item>
</style>
Himanshu
  • 454
  • 6
  • 14

3 Answers3

2

Yes, android:fontFamily does not work with AppCompatCheckbox. You can create a custom checkbox widget like this, and use it for your purpose.

public class MyCustomCheckBox extends AppCompatCheckBox {
    public MyCustomCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MyCustomCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyCustomCheckBox(Context context) {
        super(context);
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "poppins_light.ttf");
        setTypeface(tf);
    }
}
Insane Developer
  • 1,014
  • 10
  • 19
  • Is there any documentation regarding this limitation. – Himanshu Mar 09 '18 at 08:10
  • And then why is it working in layout preview of android studio? – Himanshu Mar 09 '18 at 08:14
  • 2
    Currently it is not possible. Check this thread in the Android Bug Tracker. This issue has been reported on the Android bug tracker, star it to get it fixed soon: https://issuetracker.google.com/issues/63250768 – Insane Developer Mar 09 '18 at 08:41
  • If you use new font system, replace `Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "poppins_light.ttf");`with `Typeface tf = ResourcesCompat.getFont(this.ctx, R.font.poppins_light);`. You can find more informations [on android website](https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#using-support-lib) – mrroboaat Apr 09 '19 at 08:56
0

To augment @Insane Developer's answer. If using the new xml fonts, you can use ResourcesCompat to fetch the correct typeface.

class CustomFontCheckBox : CheckBox {

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    init {
        typeface = ResourcesCompat.getFont(context, R.font.my_font)
    }
}
Tom
  • 6,946
  • 2
  • 47
  • 63
0

this issue was reported here: https://issuetracker.google.com/issues/63250768

And it was fixed on the 1.0.2 version of the AppCompat Library: https://mvnrepository.com/artifact/androidx.appcompat/appcompat/1.0.2