androidfontsandroid-stylesandroid-typeface

Android font is not applied for button via style


I created style for Button

<style name="MainButtonStyle">
    <item name="android:background">@drawable/btn_main_selector</item>
    <item name="android:textColor">@color/btn_main_text_color_selector</item>
    <item name="android:minHeight">40dp</item>
    <item name="android:textSize">18sp</item>
    <item name="android:gravity">center</item>
    <item name="android:textAppearance">@font/bold</item>
</style>

This style applies in the AppTheme

 <item name="android:buttonStyle">@style/MainButtonStyle</item>

bold.ttf file is in res/font folder

If I run an app I still see the default font. Where is the catch?


Solution

  • textAppearance allows you to define text-specific styling: font family, font color, size, etc. It is not for referencing font resource.

    Just use the fontFamily attribute to set the font in xml:

    <style name="MainButtonStyle">
        ...
        <item name="fontFamily">@font/bold</item>
    </style>
    

    or

    <style name="MainButtonTextAppearance">
        <item name="android:textColor">@color/btn_main_text_color_selector</item>
        <item name="android:textSize">18sp</item>
        <item name="fontFamily">@font/bold</item>
    </style>
    
    <style name="MainButtonStyle">
        <item name="android:background">@drawable/btn_main_selector</item>
        <item name="android:minHeight">40dp</item>
        <item name="android:gravity">center</item>
        <item name="android:textAppearance">@Style/MainButtonTextAppearance</item>
    </style>