Thursday 3 August 2017

Android Text with Custom color Outline

I was working on some sample Android App where i tried adding a small utility i.e adding a custom border to text within app with a custom color. Some times we get requirement's in apps where we need to implement such text's in views. So here's a small and useful code snippet to implement something like this (for Android Devs).

Override onDraw method for custom Textview
@Override
public void onDraw(Canvas canvas)
{
    int textColor = getTextColors().getDefaultColor();
    setTextColor(Color.BLACK);
    getPaint().setStrokeWidth(10);
    getPaint().setStyle(Paint.Style.STROKE);
    super.onDraw(canvas);
    setTextColor(textColor);
    getPaint().setStrokeWidth(0);
    getPaint().setStyle(Paint.Style.FILL);
    super.onDraw(canvas);
}


Cheers!!