androidandroid-stylesandroid-fontsandroid-switch

Setting custom font for Switch label


I've successfully set up app-wide custom font with the help of Docs and this answer. Basically I've made a new font style and referenced it in my AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    ...
    <item name="android:fontFamily">@font/custom_font</item>
    <item name="fontFamily">@font/custom_font</item> 
</style>

It is working, but not for Switch element labels. As user lxx noted in the linked answer:

This doesn't set the font everywhere... e.g. if you have styles that derive from Base.TextAppearance.AppCompat, it's not used there.

I am not sure though this is the reason behind Switches not having custom fonts.

So how do I apply this custom font to Switch elements too?

enter image description here


Solution

  • Set the typeface of switch as below:

    switchObj.setSwitchTypeface(Typeface.createFromAsset(mContext.getAssets(), "custom_font.ttf"));