# Data Binding: BindingAdapter

DevFeed: [Data Binding: BindingAdapter](<https://devfeed.tech/articles/data-binding-bindingadapter-24743.md>)

Original publisher: [Read original article](<https://medium.com/yazio-engineering/data-binding-bindingadapter-7d0824b1efff?source=rss----65bd178b00af---4>)

Author: Paul Woitaschek

Published: 2021-11-19T08:06:16Z

Content type: tutorial

Language: en

Sources: [YAZIO Engineering - Medium](<https://devfeed.tech/sources/yazio-engineering-medium.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [recyclerview](<https://devfeed.tech/topics/recyclerview.md>), [XML](<https://devfeed.tech/topics/xml.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [RxJava](<https://devfeed.tech/topics/rxjava.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [code](<https://devfeed.tech/tags/code.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [recyclerview](<https://devfeed.tech/tags/recyclerview.md>), [xml](<https://devfeed.tech/tags/xml.md>)

## AI overview

This tutorial explains how to use Android Data Binding and a BindingAdapter to apply strike-through formatting to a TextView based on a view model. The author reports reducing RecyclerView ViewHolder boilerplate by about 50%.

## Source excerpt

For a long time I did not investigate further into https://developer.android.com/tools/data-binding/guide.html but recently it just got my attention. Just like Kotlin or RxJava this was something that was constantly trying to get my attention so I decided to give it a try and to see how it works while writing my new notes app. And I must say: I'm impressed. I applied it on the ViewHolder of my RecyclerView and reduced the boilerplate code by about 50%. But there were some issues. Issues The biggest I had was manipulating the view. The grey text could be simply archieved by setting the view enabled based on the view model: android:enabled="@{!model.done()}" As the text was set to @color/abc_primary_text_material_light from the support library this automatically works. But how to set the strike through? I did not wanted to call all these `findViewById` any more as that would defeat the purpose of DataBinding. Solution It turned out there was a simple solution, called BindingAdapter> https://developer.android.com/reference/android/databinding/BindingAdapter.html Basically you just define a static method and annotate it as a binding adapter. So for strikeThrough it is: @BindingAdapter("text:strike") public static void setStrike(TextView text, boolean strike) { if (strike) { text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); } else { text.setPaintFlags(text.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); } } So now you can set it just as a regular xml attribute: android:strike="@{model.done()}" Data Binding: BindingAdapter was originally published in Yazio Engineering on Medium, where people are continuing the conversation by highlighting and responding to this story.