How can I change font weight in my project which has a default fontFamily for all textviews?

184 views Asked by At

I have a project and in this project I have defined fontFamily to all textviews in the style of AppTheme.

The fontFamily is montserrat but in text views I use montserrat_semibold, montserrat_regular and montserrat_medium.

  <style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:textViewStyle">@style/MontserratTextViewStyle</item>

        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="MontserratTextViewStyle" parent="android:Widget.TextView">
        <item name="android:fontFamily">@font/montserrat</item>
    </style>

How can I change their weight in the different XMLs of textviews?

1

There are 1 answers

1
David Kroukamp On BEST ANSWER

Hmm the only way I can think of doing that is to create multiple styles in your styles.xml:

<style name="MontserratTextViewStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">@font/montserrat</item>
</style>

<style name="MontserratTextViewSemiBoldStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">@font/montserrat_semibold</item>
</style>

<style name="MontserratTextViewRegularStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">@font/montserrat_regualr</item>
</style>

<style name="MontserratTextViewMediumStyle" parent="android:Widget.TextView">
    <item name="android:fontFamily">@font/montserrat_medium</item>
</style>

Then for any TextView which doesn't use the default montserrat i.e. maybe you want montserrat_medium:

<TextView
    style="@style/MontserratTextViewMediumStyle"
   ....
/>