It has been a long time but I'll put the static method below. Hopefully it will help another.
public static void setTextWithSpan(final TextView textView, int backgroundColor, String text, float lineSpacingMultiplier) {
class BackgroundColorSpanWithPaddingAndLineSpacing implements LineBackgroundSpan {
private float roundedCornerSize;
private int backgroundColor;
private int paddingSize;
private RectF rect;
private BackgroundColorSpanWithPaddingAndLineSpacing(int backgroundColor, int paddingSize, float roundedCornerSize) {
super();
this.backgroundColor = backgroundColor;
this.paddingSize = paddingSize;
this.roundedCornerSize = roundedCornerSize;
this.rect = new RectF();
}
@Override
public void drawBackground(Canvas c, Paint p, int left, int right, int top, int baseline, int bottom, CharSequence text, int start, int end, int currentLineNumber) {
final int textWidth = Math.round(p.measureText(text, start, end));
final int paintColor = p.getColor();
rect.set(left - paddingSize / 2, top - paddingSize / 4, left + textWidth + paddingSize / 2, top + textView.getTextSize() + paddingSize / 2);
p.setColor(backgroundColor);
c.drawRoundRect(rect, roundedCornerSize, roundedCornerSize, p);
p.setColor(paintColor);
}
}
int padding = textView.getPaddingLeft();
int radius = padding / 2;
SpannableStringBuilder builder = new SpannableStringBuilder(text);
BackgroundColorSpanWithPaddingAndLineSpacing backgroundSpan = new BackgroundColorSpanWithPaddingAndLineSpacing(backgroundColor, padding, radius);
builder.setSpan(backgroundSpan, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setShadowLayer(padding, 0, 0, 0);
textView.setLineSpacing(0, lineSpacingMultiplier);
textView.setText(builder, TextView.BufferType.SPANNABLE);
}
Usage :
SpanUtils.setTextWithSpan(titleTv, android.graphics.Color.BLUE, textStr, 1.4f);
I'm sure you'll manage to modify according to your needs.