Saturday, May 4, 2019

Adding line spacing to TextView in the presence of BackgroundColorSpan

One of my Android app's screens involves TextViews displaying text formatted with BackgroundColorSpan. That one view displays multiple lines by separating rows with newline characters. I found that the background color from one line came into contact with that of the next, an effect which looked poor. Adding line spacing in the layout XML made this worse - the background color took up the spacing and was therefore different between outer and middle lines.

I ended up fixing it by using a newline-space-newline sequence instead of just a newline to separate rows. That entire sequence of three characters is formatted with a RelativeSizeSpan to be a fifth the height of the main font. (Excluding either newline from the span makes the blank line full-height.) For padding, I also include the full sequence at the start of the string and a two-character newline-space sequence at the end.

No comments:

Post a Comment