Using Custom Fonts, AutoLink, and Justification Mode in Android TextView
This article explains how to apply custom fonts, enable AutoLink, and use justification mode in Android TextView, providing step‑by‑step code examples for assets and res/font resources, XML attributes, and runtime settings for developers.
Introduction
TextView is one of the most commonly used widgets in Android development for displaying text on the screen. It also provides several lesser‑known features that can be very useful for developers.
Custom Font
By default TextView uses the system font, but you can import your own font files and apply them either programmatically or via XML. Place the font file in res/font or assets , then use setTypeface() in code or the android:fontFamily attribute in XML (note that the XML attribute works only with system fonts and English characters).
Example code for using a custom font from assets:
// Font file placed in assets folder
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
TextView tv = findViewById(R.id.tv);
tv.setTypeface(tf);Example code for using a custom font from res/font (requires Android 8.0+):
val tv = findViewById
(R.id.tv)
val typeface = ResourcesCompat.getFont(this, R.font.myfont)
tv.typeface = typefaceCustom fonts give your app a unique look and can also be used to draw custom icons.
AutoLink
AutoLink automatically detects patterns such as email addresses, phone numbers, or URLs in the text and turns them into clickable links, removing the need to create links manually.
Enable AutoLink by setting the android:autoLink attribute to email , phone , web , or all . You can also use the Linkify class for custom link patterns.
Example XML:
When the user taps the link, the URL is opened in a browser, and you can customize the link color with android:textColorLink .
Justification Mode
The justification mode aligns text to both left and right margins by adding space between words or characters, improving readability for multi‑line English text. Set the android:justificationMode attribute to inter_word or inter_character (available on Android 8.0/API 26 and above).
Example XML:
Justification works best with multi‑line English text; it has no effect on single‑line or pure Chinese text.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.