I want to htmlize some data and set in the text view via data binding? Is it possible?
ie.If i have an extension function like this
fun TextView.htmlText(txt: String?) {
txt.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(txt, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(txt)
}
}
}
how can i put this extension function in databinding instead of this
android:text="@{model.text}"
I did it using binding adapter
@BindingAdapter("app:text")
@JvmStatic
fun htmlText(view: TextView, text: String?) {
text?.let {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
view.text = Html.fromHtml(it.trim(), Html.FROM_HTML_MODE_COMPACT)
} else {
view.text = Html.fromHtml(it.trim())
}
}
}
like this.