Last Update - 15 Nov 2019

Strike textview in android example

Strike textview in android example

Basically, the strike-through text is used for the show cut price of a product. Then we need to draw the strike text view in android. There are many scenarios in which we need to show a Strike-through text to an android user like Old prices, old offers, etc.

But there is a little problem with the android. Because Android doesn’t give any such option via XML file. If you want to show a strike-through text you can do it programming using PaintFlags. You can set paint flags Paint.STRIKE_THRU_TEXT_FLAG to a TextView and it will add a strike-through to the text.

TextView textView = (TextView) findViewById(R.id.text);
textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

There are many scenarios in which we need to show a Strike-through text to an android user like Old prices, old offers, etc. But android doesn’t give any such option via XML file.

If you want to show a strike-through text you can do it programming using PaintFlags. You can set paint flags Paint.STRIKE_THRU_TEXT_FLAG to a TextView and it will add a strike-through to the text.

But when you need it in your XML. As I am using Data Binding so my priority is to set everything related to a view in the XML. So I found this workaround on StackOverflow for my problem.

We just have to set a background that contains a line to our TextView like I did.

<TextView
                android:id="@+id/cut_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/price_bar"
                android:background="@drawable/strike_through"
                android:text="Rs. 5999"
                android:textSize="16sp"/>

 

and the drawable for the strike-through line should be like this.

<?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="#808080"/>
        </shape>
    </item>
</layer-list>

 

After using this as a bakground you will get the text as you want.

More For You

Did you find this page helpful? X