Android Development
TextView
Font Family
Android UI
Programming Tutorial

How to change fontFamily of TextView in Android

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing the font family of a TextView in Android is usually a resource and styling task, not a custom drawing task. The core decision is whether the font is static and should be declared in XML, or whether it changes dynamically at runtime and should be applied in code.

Use a Font Resource in XML

For most cases, the cleanest solution is to place the font file in res/font/ and reference it from the TextView.

xml
1<TextView
2    android:id="@+id/titleText"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:text="Hello World"
6    android:fontFamily="@font/my_custom_font" />

This is the best option when the font is static for that view. It keeps styling in layout resources and avoids unnecessary code in the activity or fragment.

Apply the Font Programmatically

If the font needs to change at runtime, load it through ResourcesCompat and assign it to the TextView.

kotlin
1import android.graphics.Typeface
2import android.os.Bundle
3import android.widget.TextView
4import androidx.appcompat.app.AppCompatActivity
5import androidx.core.content.res.ResourcesCompat
6
7class MainActivity : AppCompatActivity() {
8    override fun onCreate(savedInstanceState: Bundle?) {
9        super.onCreate(savedInstanceState)
10        setContentView(R.layout.activity_main)
11
12        val titleText = findViewById<TextView>(R.id.titleText)
13        val typeface: Typeface? = ResourcesCompat.getFont(this, R.font.my_custom_font)
14        titleText.typeface = typeface
15    }
16}

This is useful when theme settings, user preferences, or content type determine the font.

Use Styles for Consistency

If the same font should be used across multiple TextView instances, define a style instead of repeating the attribute in many layouts.

xml
1<style name="HeadingTextStyle">
2    <item name="android:fontFamily">@font/my_custom_font</item>
3    <item name="android:textStyle">bold</item>
4</style>

Then apply it:

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:text="Section Title"
5    style="@style/HeadingTextStyle" />

This keeps the typography system centralized and easier to maintain.

Think About Theme and Material Design Integration

In modern Android apps, font choice often belongs in the theme system rather than on one individual widget. If several screens share the same typography scale, theme-level styling is more maintainable than per-view overrides.

Even when you are only changing a single TextView, it is worth asking whether the font choice is really local or whether it is part of a broader app-wide type system.

Test Performance and Readability

Custom fonts are easy to add, but they still have a cost. Large font files affect app size, and aggressive font switching can complicate UI consistency. More importantly, not every decorative font is readable at small sizes or in accessibility scenarios.

A font decision is therefore part of both engineering and product design. Test the actual screens, not just one isolated TextView.

Prefer Resource Fonts Over Legacy Asset Loading

Older Android code often loads fonts from the assets/ directory with Typeface.createFromAsset. That still works, but resource fonts in res/font/ are the cleaner modern path for most apps because they integrate better with XML, styles, and resource tooling.

Use the older asset-loading style only if you have a specific reason, not as the default pattern.

Common Pitfalls

  • Repeating the same fontFamily declaration everywhere instead of using styles makes typography harder to maintain.
  • Loading fonts programmatically when the font is static adds unnecessary code and complexity.
  • Choosing fonts based only on visual novelty can hurt readability and accessibility.
  • Mixing old asset-based font loading with resource fonts without a plan creates inconsistent code paths.
  • Treating one TextView font change as isolated when it really belongs in the theme system leads to design drift.

Summary

  • For static font choices, prefer android:fontFamily with a resource font in XML.
  • For runtime font changes, load the font programmatically and assign it to the TextView.
  • Use styles or theme-level typography when the same font applies across multiple views.
  • Prefer resource fonts in res/font/ over legacy asset-loading approaches.
  • Validate font choices for readability, consistency, and accessibility, not only appearance.

Course illustration
Course illustration

All Rights Reserved.