I have a TextView that will be added programmatically. This TextView should use a SpannableString for design purposes. When adding the view, the left and right side of the span are not shown correctly because they are bigger than the TextView, which is being ignored.
How can I solve this?
This is the BackgroundColorSpan I am using: https://stackoverflow.com/a/60295758/14082516
And I add it to the View with this code:
val backgroundColorSpan = BackgroundColorSpan(Color.BLACK, 16, 10)
val spannableString = SpannableString(text)
if (text != null) {
spannableString.setSpan(
backgroundColorSpan,
0,
text.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
this.text = spannableString
You can use a workaround with paddings and fake shadow layer to expand the clipped area, please try adding the following to the View:
val spanPadding = 16 //from your snippet
...
setPadding(spanPadding, paddingTop, spanPadding, paddingBottom)
setShadowLayer(spanPadding.toFloat(), 0f, 0f, Color.TRANSPARENT)