java.lang.RuntimeException: Failed to call observer method while using binding adapter in kotlin

401 views Asked by At

I want to set binding adepter to the editText so that it always accept uppercase even if the user type lower case then it automatically convert it to upper case.For that I am using data binding and binding adapter like this.

BindingAdapter.kt

@BindingAdapter(value = ["app:alwaysCapital", "custom:holderAttrChanged"], requireAll = false)
fun setHolder(editText : EditText,text: String?, listener : InverseBindingListener) {
    
    if (text != null) {
        editText.setText(text)
        editText.setSelection(text.length)
    }
    listener.onChange()
    editText.doOnTextChanged{ _ : CharSequence?, _ : Int, _ : Int, _ : Int ->
        listener.onChange()
    }
}
@InverseBindingAdapter(attribute = "app:alwaysCapital", event = "custom:holderAttrChanged")
fun getHolder(editText : EditText) : String? {
      return editText.text.toString().toUpperCase()
}

EditText xml

  <EditText
        android:id="@+id/dfsc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:hint="@string/lname"
        app:alwaysCapital="@={viewModel.txtName}"
        android:imeOptions="actionNext"
        android:inputType="text" />

And in ViewModel

   val txtName = MutableLiveData<String>()

When I open this activity it throws error as mention in below image. enter image description here

0

There are 0 answers