Can I create a strikethrough text in Android, I mean adding a special value in the TextView
tag that can make this possible?
If you are using Kotlin:
your_text_view.apply {
paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
text = "Striked thru text"
}
In your observable view model
fun getStrikeContent(): String {
return "Hello"
}
companion object {
@BindingAdapter("strike")
@JvmStatic
fun loadOldPrice(view: TextView, value: String) {
view.text = value
view.paintFlags = STRIKE_THRU_TEXT_FLAG
}
}
then in your xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:strike="@{itemViewModel.strikeContent}" />
In Kotlin you can create extension property:
inline var TextView.strike: Boolean
set(visible) {
paintFlags = if (visible) paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
else paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
get() = paintFlags and Paint.STRIKE_THRU_TEXT_FLAG == Paint.STRIKE_THRU_TEXT_FLAG
And use:
textView.strike = true
It is really easy if you are using strings:
<string name="line"> Not crossed <strike> crossed </strike> </string>
And then just:
<TextView
...
android:text="@string/line"
/>
Just use this and you are done . For Activity :
TextView t= (TextView).findViewById(R.id.thousand));
t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
For Xml :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_view_original_cash_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="@android:color/darker_gray"
android:text="Rs. 1,999"/>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_centerVertical="true"
android:layout_alignStart="@id/text_view_original_cash_amount"
android:layout_alignEnd="@id/text_view_original_cash_amount"
android:layout_alignLeft="@id/text_view_original_cash_amount"
android:layout_alignRight="@id/text_view_original_cash_amount" />
</RelativeLayout>