Search code examples
androiddata-bindingandroid-edittext

Android: custom view data binding: bind values from system attrs.xml


I want to pass values like EditText's imeOption and inputType to a custom view (which contains an EditText) using @BindingAdapter so it can be customizable from the view that will be using it.

Ideally this would go:

@JvmStatic
@BindingAdapter("customEditText:imeOptions")
fun setImeOption(view: CustomEditText, inputVariable: Int) {
    view.binding.editText.imeOptions = inputVariable
}

and in xml:

<CustomEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    customEditText:imeOptions="@{actionDone}"/>

but actionDone cannot be resolved. It there any way to do this?

Note: CustomEditText does not extend EditText, it's just a LinearLayout which contains an EditText among other views


Solution

  • Within the data tags, import the EditorInfo class:

    <data>
        <import type="android.view.inputmethod.EditorInfo"/>
    </data>
    

    And set it like:

    customEditText:imeOptions="@{EditorInfo.IME_ACTION_DONE}"