Search code examples
androiddata-bindingandroid-databinding

How to refrence another view sibling as adapter argument in data binding


I have 2 edit texts in my view and I'm using data binding. What I want to achieve is whenever the first text view had 5 characters, the focus passes on to the next edit text, and whenever that edit text also had 5 characters, the focus should be removed from the whole view.

This is the code I wrote for my binding adapter:

@BindingAdapter("setMaxLength", "nextPart")
fun EditText.onTextChange(maxLength: Int, nextPart: EditText) {
    filters = arrayOf<InputFilter>(InputFilter.LengthFilter(maxLength))
    addOnTextChangeListener {
        if (it.length == maxLength) {
            clearFocus()
            nextPart.requestFocus()
        }
    }
}

I don't know how I should pass these 2 arguments to my function in xml.

            <androidx.appcompat.widget.AppCompatEditText

                android:id="@+id/firstPart"
                nextPart="alphabet"
                setMaxLength="@{5}" />

            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/alphabet"
                setMaxLength="@{5}" />

This code has build issues with error:

Cannot find a setter for <androidx.appcompat.widget.AppCompatEditText setMaxLength> that accepts parameter type 'int'

If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

I'm not sure how I could do this, also addOnTextChangeListener is a text watcher and the functionality is tested.

Am I even on the right path here? Any ideas would be appreciated.


Solution

  • Try doing it like this

     <androidx.appcompat.widget.AppCompatEditText
    
                android:id="@+id/firstPart"
                nextPart="@{alphabet}"
                setMaxLength="@{5}" />