Reputation: 502
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>
Upvotes: 0
Views: 751
Reputation: 502
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>
Upvotes: 1