MaYar
MaYar

Reputation: 91

How to make a strike through text using DataBinding?

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

Answers (2)

mtrakal
mtrakal

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

Gavin Wright
Gavin Wright

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

Related Questions