3

I am new to android. I tried the below code for strikethrough. But how can I change the color of the strikethrough(currently it is BLACK, I want it RED). I know its probably simpler but I could not find it even after googling much. Please help.Thanks in advance.

txtview.setText("Hello");
txtview.setPaintFlags(txtview.getPaintFlags()|Paint.STRIKE_THRU_TEXT_FLAG);
Anand.B
  • 141
  • 1
  • 2
  • 11

1 Answers1

5

I think this is not possible for simple textview so you have to do the following:-

1.Create a custom TextView by extending View class

2.Declare this custom textview inside XML layout same like we do for TextView.

And at last write an onDraw() method like following.

    @Override
       protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(strikeThroughColor);
        paint.setStyle(Paint.Style.FILL); 
        paint.setStrikeThruText(true);
        paint.setStrokeWidth(strikeThroughWidth);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        super.onDraw(canvas);
        float width = getWidth();
        float heigh = getHeight();
        canvas.drawLine(width/10, heigh/10, (width-width/10),(heigh-heigh/10), paint);
}

Hope it will helps you.

Born To Win
  • 3,319
  • 3
  • 19
  • 27