black line around the text

Ask by : Max Usanin November 04, 2012 18:03

Answer by : SimonNovember 04, 2012 18:03

Extend the TextView class. Then in the onDraw, draw the text first using black, then draw it again, slightly smaller and using white. For extra "correctness", add custom attributes to the XML to set the "line around" colour.

public class myTextView extends TextView{

    public myTextView (Context context) {
        this(context, null);    

    public myTextView (Context context, AttributeSet attrs) {
        this(context, attrs, 0);            


    public myTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);        
        // do extra initialisation and get attributes here

    protected void onDraw(Canvas canvas) {

        // draw first in black
        Paint paint = new Paint();
        paint.setTextSize(20);                // text size

        canvas.drawText("My text", 50, 50, paint);

        // draw again in white, slightly smaller
        paint.setTextSize(18);                // text size

        canvas.drawText("My text", 50, 50, paint);



The code is not complete as it hardcodes the colours, size and positions but I hope that it is enough for you to work with. You can get the text size and text colour from the XML via attrs in the constructor and add line colour and width as custom attributes (search here or Google).

View original answer