2

I am trying to create textview with text color as black and strikthrough as red, I tried to use html but does not seems to work

String styledText = "<span style='color:red;text-decoration:line-through'><span style='color:black'>TEXT</span></span>";    
myText.setText(Html.fromHtml(styledText));

I also tried below method but don't know how do define different color for strikethrough

myText.setPaintFlags(myText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
mkso
  • 3,178
  • 4
  • 27
  • 35

3 Answers3

2

If you want to strike through all the text in the TextView you can simply create a sub-class of TextView in which you draw the strike line with the color of your choice.

slayton
  • 20,123
  • 10
  • 60
  • 89
  • @mkso: Actually, I don't think it's going to get much easier than slayton suggested. Internally any strikethrough text will be represented by a `StrikeThroughSpan`, which on its turn sets a flag on a `(Text)Paint` object. That class, unfortunately, uses native code for drawing and I don't see any variables in the Java source code that can influence the color of the strike (which you already seems to have discovered yourself). I'm afraid you're going to have to do the drawing manually if you want it to be different colour. – MH. Apr 19 '12 at 23:52
  • @mkso, this is a simple solution, basically the only method you have to override is the `onDraw` method. First call `super.onDraw()`, then set the strike through color and then draw the line. Its really easy – slayton Apr 20 '12 at 00:33
2
@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    drawStrikeThroughPaint(canvas);
}

public void drawStrikeThroughPaint(Canvas canvas) {
    canvas.drawLine(0, getMeasuredHeight() / 2, getMeasuredWidth(), getMeasuredHeight() / 2, strikethroughPaint);
}
chrem
  • 31
  • 1
1

late to the party but use a layer-list and place it on top of the textview background. works perfectly. Let me show you.

create in the drawables folder a file called strikethru.xml:

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF"/>
        </shape>
    </item>
    <item>
        <shape android:shape="line">
            <stroke android:width="1dp"
                android:color="#8d8d8d"/> <!-- adjust color you want here -->
        </shape>
    </item>
</layer-list>

then in your textview do this:

    <TextView
                                android:id="@+id/tv_toolbar_prod_price"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"

                                android:paddingRight="3dp"
                                android:text="1,290 B"      

android:background="@drawable/strikethru_line"
                                android:textColor="#070707"
                                android:textSize="13sp" />

the padding right of 3dp makes the strike through come out more from the text to give a real world effect.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Setting it as the background will draw behind the text and not on top of it. Better solution would be to set it as foreground, but android 5 has some measurement issues and the forground cannot be tinted on lower api levels – Jakob Ulbrich Dec 04 '18 at 14:48