Joel Christophel Joel Christophel - 6 months ago 49
Java Question

Text overline in Android

Is there any way to put a line over text in Android? Since underlines and strikethroughs are possible (independent of font), it seems that overlines should be too.

I've tried using the combining overline symbol (pictured below), but it definitely looks funky. Is there a different way to achieve this?

Roman Numeral Converter

Answer

I know this is not a perfect solution. but it will give some Idea...

public class OverLineTextView extends TextView {

    private Paint paint;

    public OverLineTextView(Context context) {
        super(context);
        init();
    }

    public OverLineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OverLineTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float width = getPaint().measureText(getText().toString());
        canvas.drawLine(getTotalPaddingLeft(), getTotalPaddingTop() + 1,
                getTotalPaddingLeft() + width, getTotalPaddingTop() + 1, paint);
    }
}
Comments