Android Developers
Published in

Android Developers

Illustration by Virginia Poltrack

Spantastic text styling with Spans

Styling text in Android

Left: Single style text. TextView with textSize=”32sp” and textStyle=”bold”. Right: Multi style text. Text with ForegroundColorSpan, StyleSpan(ITALIC), ScaleXSpan(1.5f), StrikethroughSpan.
Left: Using HTML tags. Center: Using BulletSpan with default bullet size. Right: Using BulletSpan on Android P or custom implementation.
Combining TextView with XML attributes and text with spans

Applying Spans

Left: Text with ForegroundColorSpan. Right: Text with ForegroundColorSpan and Spannable.SPAN_EXCLUSIVE_INCLUSIVE
Text with multiple spans: ForegroundColorSpan(Color.RED) and StyleSpan(BOLD)

Framework spans

Span categories: character vs paragraph, appearance vs metric
Appearance affecting spans
Metric affecting spans
Left: ForegroundColorSpan — appearance affecting span. Right: RelativeSizeSpan — metric affecting span
On Android paragraphs are defined based on new line (‘\n’) character.
Paragraph affecting spans
Left: BackgroundColorSpan — character affecting span. Right: QuoteSpan — paragraph affecting span

Creating custom spans

Testing custom spans implementation

Testing spans usage

Note: if you need to iterate through the spans outside tests, use Spanned#nextSpanTransition instead of Spanned#getSpans as it’s more performant.

Lots of thanks to Siyamed Sinir, Clara Bayarri and Nick Butcher.




Articles on modern tools and resources to help you build experiences that people love, faster and easier, across every Android device.

Recommended from Medium

CA7 Command Reference — LQ Command example.


Modernizing IBM i System Through API Integration

API integration in IBM i systems

Min-width , Max-width & Media Queries

Message Integrity, Authentication, and Non-Repudiation

AWS CodeDeploy, Auto Scale Groups, and debugging recursive deployment limbo.

Asynchronous Communication

Configuration of Apache Web Server in Docker Container using Ansible.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Florina Muntenescu

Florina Muntenescu

Android Developer Advocate @Google

More from Medium

Android Touch System — Part 1: Touch Functions and the View Hierarchy

Prep your TV app for Android 12

The 3 Jetpack Compose Animation Transition Low-Level APIs

Intro showcase view in Jetpack Compose