Question

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

Was it helpful?

Solution

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);
    }
}

OTHER TIPS

After doing enough R & D, the only way for doing that is to create a ImageView and set it dynamically to a line image and set its width with respect to the width of the textview by using getWidth() and position above the textview and align left with respect to the textview

If you want a strikethrough on your textview ,you can do using paint flags

TextView textview1=(TextView) findViewById(R.id.textView1);
textview1.setPaintFlags(textview1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top