Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.support.v7.widget.AppCompatCheckBox fontfamily not applied on runtime

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>
like image 510
Himanshu Avatar asked Nov 20 '25 19:11

Himanshu


1 Answers

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);
    }
}
like image 60
Insane Developer Avatar answered Nov 23 '25 09:11

Insane Developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!