I'v added Text Stroke for TextView using ReplacementSpan. It's working perfectly in Android Sdk below Marshmallow. But, In marshmallow it showing blank text. When I'v added ForegroundSpan to Top TextView and then set my custom span to below views it work's. I don't understand is there anything I'm missing.
Below is my TextSpannable Class code -
public class TextSpannable extends ReplacementSpan {
private final Paint paintFill = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint paintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Path path = new Path();
private int width;
public TextSpannable(int strokeWidth) {
paintFill.setColor(Color.WHITE);
paintStroke.setStyle(Paint.Style.STROKE);
paintStroke.setStrokeWidth(6);
}
public void setPathEffect(PathEffect effect) {
paintStroke.setPathEffect(effect);
}
@Override
public int getSize(Paint paint, CharSequence text, int start,
int end, Paint.FontMetricsInt fm) {
this.paintFill.setColor(Color.WHITE);
this.paintStroke.setColor(Color.parseColor("#402002"));
width = (int) (paint.measureText(text, start, end) +
this.paintStroke.getStrokeWidth() + paintFill.getTextSize());
return width;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y,int bottom, Paint paint) {
path.reset();
paint.getTextPath(text.toString(), start, end, x, y, path);
path.close();
canvas.translate(this.paintStroke.getStrokeWidth() / 2, 0);
canvas.drawPath(path,this.paintStroke);
canvas.drawPath(path, this.paintFill);
canvas.translate(-this.paintStroke.getStrokeWidth() / 2, 0);
//canvas.drawText(this.text,start,end,x,y,paintFill);
//canvas.drawText(this.text,start,end,x,y,paintStroke);
}
}
This is how I set it to TextView -
spannableString7 =
new SpannableStringBuilder(getString(R.string.string1));
ForegroundColorSpan fcs =new ForegroundColorSpan(Color.TRANSPARENT);
spannableString7.setSpan(fcs,0, getString(R.string.string1).length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textSpam.setText(spannableString7, TextView.BufferType.SPANNABLE);
//when I comment above block, text becomes blank in Textview
spannableString1 =
new SpannableStringBuilder(getString(R.string.string1));
spannableString1.setSpan(textSpannable,0,getString(R.string.string1).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString1, TextView.BufferType.SPANNABLE);