Trying to call a method in a data binded xml file ...
# android-databinding
l
Trying to call a method in a data binded xml file where I want to first create a new object in Kotlin through it’s companion function and let it resolve to setText. Running into a compiler issue, do I need to add an annotation to expose this somehow to the xml? Path to
Money
is imported in the <data> tags.
android:text="@{Money.Companion.fromCents(state.cents)}"
Copy code
`
****/ data binding error ****msg:cannot find method fromCents(java.math.BigDecimal) in class...
Copy code
companion object {

        fun fromCents(pennies: BigDecimal): Money {
            return Money(pennies.divide(BigDecimal(100)))
        }
    }
r
Wouldn’t it be safer/easier to use a @BindingAdapter
You’d have:
app:fromCents="@{state.cents}"
with
Copy code
object MoneyDataBinding
{
    @BindingAdapter("fromCents")
    @JvmStatic
    fun fromCents(view: TextView, pennies: BigDecimal)
    {
        // magic
    }
}
l
I figured it out, Just can’t call
Companion
in the XML 🙂
It’s a one off, don’t feel like creating a binding adapter 🙂