Reputation: 91
I have a TextView and CheckBox, and I want it to work in such way:
If I was using ViewBinding I would write it like this:
binding.textViewName.paint.isStrikeThruText = task.completed
The problem is to set this strike using DataBinding.
Is there a way to do it using "pure" DataBingding or it is necessary to use some adapters?
Upvotes: 1
Views: 312
Reputation: 6417
You can create a custom BindingAdapter as kotlin extension:
@BindingAdapter("strikeThrough")
fun TextView.strikeThrough(strikeThrough: Boolean = true) {
this.paintFlags = this.paintFlags or
if (strikeThrough) {
Paint.STRIKE_THRU_TEXT_FLAG
} else {
Paint.STRIKE_THRU_TEXT_FLAG.inv()
}
}
Usage in XML layout:
<TextView
app:strikeThrough="@{true}"
.../>
Upvotes: 1
Reputation: 3212
There's no way to draw strikethrough text in XML. You'll need a BindingAdapter.
Or just go back to using view binding! The community has largely turned away from data binding.
Upvotes: 0