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.
So, How we can set a text in TextView to show a Strike-through text via DataBinding?
- First of all, create TextView in our Layout
1 2 3 4 5 6 7 8 9 |
<TextView android:id="@+id/list_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@{products.formatlistPrice}" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textColor="@color/primary_color" tools:text="@string/price"/> |
- and Return SpannableString from our Getter method
1 2 3 4 5 |
public SpannableString getFormatlistPrice() { SpannableString formatListPrice = new SpannableString(this.formatlistPrice); formatListPrice.setSpan(new StrikethroughSpan(), 0, this.formatlistPrice.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); return formatListPrice; } |