0

I have a button and I want to set a custom font to the text but I have trouble doing that.

The code is the following :

In .xml file :

<Button
        android:id="@+id/button"
        style="@style/ButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="Sample Text"/>

and in styles.xml

<style name="ButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless">
        <item name="android:textSize">@dimen/button_text_size</item>
        <item name="android:background">@drawable/button_background</item>
        <item name="android:textAppearanceButton">@style/ButtonTextStyle</item>
        <item name="android:textColor">?android:textColorPrimaryInverse</item>
    </style>

<style name="ButtonTextStyle" parent="TextAppearance.AppCompat">
        <item name="android:fontFamily">@font/my_custom_font</item>
    </style>
17
  • So what does not work? Commented Mar 5, 2019 at 8:27
  • @ZUNJAE the font is not set using the above code. I updated the description Commented Mar 5, 2019 at 8:32
  • What guide are you following for setting a custom font? Commented Mar 5, 2019 at 8:38
  • 1
    @MihaiBC if you change the font in the code-behind, will it work? Try calling setTypeface on your button Commented Mar 5, 2019 at 9:04
  • 1
    You can post the solution as answer so that more people can see it. Commented Mar 6, 2019 at 1:30

1 Answer 1

1

Instead of of creating a different style fontFamily should be used in the button style and the code should look like this.

<style name="ButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless">
    <item name="android:textSize">@dimen/button_text_size</item>
    <item name="android:background">@drawable/button_background</item>
    <item name="android:fontFamily">@font/my_custom_font</item>
    <item name="android:textColor">?android:textColorPrimaryInverse</item>
</style>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.