ReplacementSpan's draw() method isn't called

android, spannablestring

Solution

In case anyone has this problem, I encountered it. What I had to do is to pass a second parameter into the setText() method. My call looked like

textView.setText(spanned, TextView.BufferType.SPANNABLE);

Hope this helps!

Problem

I set background in string like that: ``` spanString.setSpan(new BackgroundColorSpan(color), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ``` But I would like to increase left and right padding in this background so I created custom span ``` public class PaddingBackgroundSpan extends ReplacementSpan { private int mBackgroundColor; private int mForegroundColor; public PaddingBackgroundSpan(int backgroundColor, int foregroundColor) { this.mBackgroundColor = backgroundColor; this.mForegroundColor = foregroundColor; } @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { return Math.round(measureText(paint, text, start, end)); } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom); paint.setColor(mBackgroundColor); canvas.drawRect(rect, paint); paint.setColor(mForegroundColor); canvas.drawText(text, start, end, x, y, paint); } private float measureText(Paint paint, CharSequence text, int start, int end) { return paint.measureText(text, start, end); } ``` I use my span by: ``` spanString.setSpan(new PaddingBackgroundSpan(color1, color2), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); ``` Unfortunately, my draw() method isn't called. getSize() is called correctly.

Original source