mkso
mkso

Reputation: 3188

create different color strike through

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

Upvotes: 2

Views: 3517

Answers (3)

j2emanue
j2emanue

Reputation: 62529

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.

Upvotes: 1

chrem
chrem

Reputation: 31

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

Upvotes: 2

slayton
slayton

Reputation: 20319

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.

Upvotes: 2

Related Questions