3

This is my AppTheme style in style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:fontFamily" tools:targetApi="jelly_bean">@font/montserrat_regular</item>
    <item name="fontFamily">@font/montserrat_regular</item>
</style>

and the application tag in manifest.xml

 <application
        android:name=".Amelio"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"

What i want is to set fontFamily in AppTheme so i dont have to set it on every component in app. But i want to set custom font on some of TextView in app.

Issue is when i set fontFamily in AppTheme then i can not set font or fontFamily on any TextView. But i can set that when i remove fontFamily and font from AppTheme. Here is the preview -

  1. with font attribute in AppTheme style

with font in AppTheme

  1. without font attribute in AppTheme style

with font in AppTheme

Note: I know either font or fontFamily can be used, i was just trying both if works. so please ignore it.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212

1 Answers1

0

Khemraj is correct in suggesting https://stackoverflow.com/a/16407123/6891563, because when you want to override some component's property you need to override its style, so when you add (taked from Hussein El Feky):

<!-- Into a styles.xml--!>
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">sans-serif-light</item>
</style>

<!-- Into a themes.xml--!>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textViewStyle">@style/RobotoTextViewStyle</item>
    <item name="buttonStyle">@style/RobotoButtonStyle</item>
</style>

You are overriting all your TextViews' styles, so whenever you use AppTheme, TextView components will listen to RobotoTextViewStyle properties and use them.

Jatezzz
  • 153
  • 2
  • 8