Search code examples
androidkotlinmvvmandroid-databinding

InverseBindingAdapter not working, Getting error: <identifier> expected in generated Java code


I am trying to create a Custom binding adapter to convert EditText text to Long and reverse.

This is my bindingAdapter class

class CustomBindingUtils {

    @BindingAdapter("android:text")
    fun setLong(editText: EditText, value: Long) {
        editText.setText(value.toString())
    }

    @InverseBindingAdapter(attribute = "android:text")
    fun getLong(editText: EditText): Long {
        val textValue = editText.text.toString()
        if (textValue.isDigitsOnly()) {
            return textValue.toLong()
        }
        return 0L
    }

}

View that uses the custom binding adapter:

<EditText
    android:id="@+id/payeeAmountInput"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:background="@drawable/bg_rounded_green"
    android:inputType="numberDecimal"
    android:padding="8dp"
    android:text="@={payee.amount}"
    android:textAlignment="textEnd"
    app:layout_constraintLeft_toRightOf="@id/payeeNameInput"
    app:layout_constraintRight_toLeftOf="@id/removePayeeButton"
    app:layout_constraintTop_toTopOf="parent" />

But when I click on build, I am getting the following error from the generated Java class.

/home/xxxxx/Works/AppTest/app/build/generated/source/kapt/debug/com/xxxxxx/groupsplit/databinding/LayoutPayeeItemBindingImpl.java:31: error: <identifier> expected
            long callbackArg_0 = mBindingComponent.null.getLong(payeeAmountInput);
                                               ^

Am I doing anything wrong here?


Solution

  • You need to wrap the BindingAdapter into a companion object and annotate @JvmStatic:

    companion object {
        @BindingAdapter("android:text")
        @JvmStatic
        fun setLong(editText: EditText, value: Long) {
            editText.setText(value.toString())
        }
    }