Custom fonts and XML layouts Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Custom fonts are one of the easiest ways to change the tone of an Android interface, but they only stay maintainable if they are wired into layouts cleanly. In modern Android projects, the right default is to treat fonts as resources, reference them from XML, and only use code when the typeface must change at runtime.
Put Fonts in res/font
The modern Android resource-based approach is to store font files in res/font. Once the file is there, Android generates a resource id for it, just like it does for strings and drawables.
For example:
- '
res/font/inter_regular.ttf' - '
res/font/inter_bold.ttf'
become:
- '
@font/inter_regular' - '
@font/inter_bold'
This is much cleaner than the older assets/fonts pattern because the resource is compile-time checked and works directly in XML.
Reference the Font in XML
If a TextView always uses the same font, keep that decision in the layout:
This keeps typography visible in the XML where designers and developers expect to find it.
Use Font Family XML for Weights and Variants
If your design needs multiple weights from the same family, define a font family resource instead of scattering separate files across views.
Now views can point at the family rather than individual files. This works better when you want Android to resolve normal versus bold usage from one logical family.
Prefer Styles for Repeated Layout Use
If several layouts use the same typography, define a style instead of repeating android:fontFamily everywhere.
Then apply it:
This matters because font choices rarely travel alone. They usually come with size, weight, spacing, and color decisions.
Load Fonts in Code Only When the Choice Is Dynamic
If the font depends on runtime state such as a theme toggle or user preference, set it in code:
That is still resource-based, which keeps the implementation safer than manually loading files from assets.
Fonts Change Layout Behavior
Typography is not separate from layout. A custom font changes width, line height, and readability. A layout that looked correct with the default system font can start clipping, wrapping awkwardly, or feeling visually cramped after the font changes.
For that reason, validate custom fonts in the real XML layouts that use them. In practice, that means:
- avoid fixed heights for text-heavy views
- prefer
spfor text size - test long strings and translated strings
- check accessibility font scaling
If the layout is rigid, the font will expose the weakness.
Common Pitfalls
- Mixing the old
assets/fontspattern with the modernres/fontresource pattern. - Using a custom font without bundling all the weights and styles the design expects.
- Setting fonts in code when the choice is static and belongs in XML or styles.
- Forgetting that custom fonts affect wrapping and spacing, not only appearance.
- Choosing decorative fonts that hurt readability under localization or accessibility scaling.
Summary
- Put bundled fonts in
res/fontso Android treats them as normal resources. - Use
android:fontFamilyin XML for static typography choices. - Define font-family resources and styles when several views share related type settings.
- Load fonts in code only when the typeface is decided at runtime.
- Always recheck the surrounding layout because typography changes sizing and flow.

