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.
1 2 |
TextView textView = (TextView) findViewById(R.id.text); textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); |
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.
1 2 3 4 5 6 |
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/strike_through" android:padding="5dp" android:text="@{prod.old_price}"/> |
and the drawable for the strike-through line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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> |
and you will get the text as you want.