Svigg
Svigg

Reputation: 21

How to set multiple font size defaults across android app?

I'm curious if it's possible to make specialize default font sizes that you can reference across your layout xml files. I'm looking for something similar to how you can make a color resource file. I want to make something so that I could say "@fonts/title", "@fonts/subtitle" or "@fonts/content-small" in my xml layouts.

Upvotes: 1

Views: 403

Answers (2)

parthpatibandha
parthpatibandha

Reputation: 92

Define the text size as per the material design guideline.

<dimen name="text_header_regular">24sp</dimen>
<dimen name="text_header_medium">20sp</dimen>
<dimen name="text_body">16sp</dimen>
<dimen name="text_body2">14sp</dimen>
<dimen name="text_caption">12sp</dimen>
<dimen name="text_overline">10sp</dimen>
<dimen name="text_overline_2">8sp</dimen>

Create the custom styles.

<style name="tvStyleBold">
    <item name="android:fontFamily">@font/montserrat</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@color/colorTextPrimary</item>
    <item name="android:textSize">@dimen/text_body</item>
</style>

<style name="tvStyleRegular">
    <item name="android:fontFamily">@font/montserrat</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@color/colorTextPrimary</item>
    <item name="android:textSize">@dimen/text_body2</item>
</style>

Then your textview look like this.

<androidx.appcompat.widget.AppCompatTextView
     style="@style/tvStyleBold"
     android:text="@string/change_password_com"/>

Upvotes: 1

Rediska
Rediska

Reputation: 1470

You may define a style resource like this:

<style name="title" parent="TextAppearance.AppCompat">
  <item name="android:textSize">20sp</item>
</style>

In a layout file, you can add android:textAppearance or style attribute to a TextView:

 <TextView
   ...
   android:textAppearance="@style/title">

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

Upvotes: 1

Related Questions