Sunday, April 18, 2010

Underlined text in Android

Again, I found it surprisingly hard to find a TextStyle for underlined text in a standard TextView. After spending Too Long navigating API reference, it dawned on me that good-ol' OO concepts can come to the rescue--sub-classing. So, here's a neat little class that achieves underlining in a TextView:


public static class TitleTextView extends TextView
{
public TitleTextView(Context c)
{
super(c);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
}

Paint mPaint;

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);

// underline the text
canvas.drawLine(0, super.getHeight() - 1, super.getWidth(),
super.getHeight() - 1, mPaint);
}
}