How to draw text with different stroke and fill colors?

Yugandhar Babu picture Yugandhar Babu · Jan 28, 2012 · Viewed 25.6k times · Source

I want to display text as below in my app. I am using Paint class with style FILL_AND_STROKE to achieve this. But only one method setColor() is available to set the color.

How do I set different stroke and fill colors?

text with different stroke and fill colors

Answer

Inside custom TextView (does not work in EditText):

@Override
public void onDraw(Canvas canvas)
{
    final ColorStateList textColor = getTextColors();

    TextPaint paint = this.getPaint();

    paint.setStyle(Style.STROKE);
    paint.setStrokeJoin(Join.ROUND);
    paint.setStrokeMiter(10);
    this.setTextColor(strokeColor);
    paint.setStrokeWidth(strokeWidth);

    super.onDraw(canvas);
    paint.setStyle(Style.FILL);

    setTextColor(textColor);
    super.onDraw(canvas);
}