Published September 21, 2015

Rich TextView styling with spans for Android

I was recently inspired by Lisa Wray and her presentation at the Droidcon NYC conference – Beautiful Typography on AndroidI am going to show you how to use spans to enrich the TextView component. It’s based on a true story 🙂

My project Omni, that I am working on at Schibsted Tech Polska, requires some rich styling. Being a news app there is a lot of text and images to handle.

To make it work as our designer wished, I had to implement a custom component and take care of measurement, layout and drawing. The component wraps text around images, uses 3 different custom fonts, multiple colors, draws colored dots, etc.

I will focus here on a simpler use case where I was able to use spans instead. Below I have marked my custom component with a red rectangle. Instead of custom view I am going to use single TextView.

omni_text_ui

SPANS, WHAT IS THAT?

If you ask a regular Android developer about spans, you will probably hear: What is that? 

It is not because we love plain texts. It is because it is not well documented, there is a lack of tutorials, trainings and so on.

When I looked at the Android API searching for spans, I found many classes that include the span word, but I couldn’t get a clear idea about how this stuff works.

Fortunately, thanks to Lisa and other materials, I was able to start playing with spans and realised that it is very easy and powerful.

I am not going to explain all details about spans and possible scenarios. I just want you to have a basic understanding and show how it could be used. It is up to you to expand your knowledge and become a Spans expert.

So, think of a single span as an attribute that can be applied to a single character or a paragraph. These “attributes” define the text formatting (metrics/size/appearance). When you apply a span to a part of the text, you have to specify a range of indexes (start and end of that subtext). You can apply multiple spans to the same range of text.

TO-DO

Let us start splitting the text into 3 parts:

  • colored dot (CenteredImageSpan)
  • vignette with light gray color, 13sp size and custom font Benton -Medium (CustomTypefaceSpan, TextAppearanceSpan)
  • article text with black color, 15sp size and custom font Benton – Regular (use TextView defaults)

Note that the code presented below is written in Kotlin. It is my primary language now, but it’s not that different from Java. So any Android developer should be able to understand that code.

COLORED DOT

ImageSpan – is a very useful span that replaces text with a specified image. That was my first choice for colored dot. Unfortunately, it is aligned only to the bottom or the baseline of the line and I needed to center my dot vertically. So I decided to write my own span. Looking at some StackOverflow answers I considered extending ImageSpan. However, that seemed to be too complex for such a simple task. ImageSpan only loads drawables from different locations. More interesting are parents of that class such as DynamicDrawableSpan and ReplacementSpan.

DynamicDrawableSpan – creates a weak reference to a once loaded drawable. This is an optimisation for drawables loaded from anywhere else than the memory. It also calculates the span size and draws it.

Since I load the drawables from the memory, I don’t need caching. I therefore decided to override ReplacementSpan directly and copy the getSize() and draw() methods from DynamicDrawableSpan.

The getSize() method is almost not changed at all, I just added a gap parameter so I can make some extra space between the colored dot and the rest of text.

The draw() method is changed a bit to move colored dot up enough to be centered vertically

VIGNETTE

To be able to apply a custom font from the assets, you have to provide your own span. This is very easy . Thanks again to Lisa Wray for providing the code! The implementation was super easy,  but I needed to know which class should be extended.

Lisa explains that class best in her presentation.

To specify a size and color we can use the built in class TextAppearanceSpan. First we create the new style.

Then we can use a class constructor to create a new span

ARTICLE TEXT

In the case article text we don’t need to specify any spans. We can set the TextView properties either in XML or programatically.

PUT IT ALL TOGETHER

To make it all work we need SpannableString that handles spans. For our convenience we can use the builder class SpannableStringBuilder.

Because there is no append() method that takes multiple spans, I created my own:

That’s it. End of story 🙂