I have an app where I am displaying multiple lines, and multiple paragraphs, of formatted text in a TextView
. I am using SpannableStringBuilder
for this purpose.
One of the things I want to do here is to be able to highlight the text. Now I have tried using BackgroundColorSpan
, but in this case, the background on the text covers the complete line-height. I want it to cover only the text instead. There seems to be no apparent way to set a vertical padding or height on this span. Just the colour.
Secondly, I also tried subclassing ReplacementSpan
and implementing a backgroundSpan
of my own by drawing in the draw method of this class. But this doesn't seem to support multiline highlighting.
Can anyone tell me how I can go about achieving this highlighting functionality? Basically, I want it to work like an ebook reader, preferably the Kindle
or the default Book reader on Android
.
I tried the solution of koopuluri, but the LineBackgroundSpan is for an entire paragraph when just I wanted to highlight some words. So I found the ReplacementSpan, where we can override the function
draw
and draw a rectangle behind our text. You have to give it the text height.