my extension function for TextView:
fun TextView.setColorifiedText(text : String) {
// Some logic to change color of text
setText(text)
}
my xml file with databinding:
<layout>
<data>
<!--some data-->
</data>
<LinearLayout>
<TextView
android:setColorifiedText="some text to be colorified"
/>
</LinearLayout>
</layout>
Yes, it is possible to access an extension function written for a view in XML.
We have prefix the extension function name with 'set' and annotate it with 'BindingAdapter' annotation., for ex:- if your extension function name is 'colorText' change it to 'setColorText' and access the same in XML with the attribute name 'colorText' from 'app:' namespace. Also, annotate the extension function with @BindingAdapter("colorText")
@BindingAdapter("colorText")
fun TextView.setColorText(text : String) {
// logic to something with the text
}
in XML :
<TextView
app:colorText="your String" />
you can also modify the code using the kotlin's setter getter to advantage
In case of java declare the function as static and add an argument to the method which gets reference to the textview you gave the attribute to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With