Edittext cursor resetting to left after Android Data Binding update
Your best bet here is to use a custom @BindingAdapter
which will already have a reference to the EditText. That way you can avoid re-binding if the text in the EditText
matches your model, which will resolve your cursor issue.
First, change android:text="@{user.name}"
to bind:binding="@{user.name}"
. Then, add this static method anywhere in your project. We keep all of them in a class called BindingAdapters.java
. By the way, starting in RC2 you can create non-static binding adapter methods, but that probably isn't your biggest concern right now.
@BindingAdapter("binding")
public static void bindEditText(EditText editText, CharSequence value) {
if (!editText.getText().toString().equals(value.toString())) {
editText.setText(value);
}
}
To fix the weird data binding behaviour that resets the cursor to the start of the EditText, you can add the following InverseBindingAdapter :
@SuppressLint("RestrictedApi")
@BindingAdapter("android:text")
public static void setText(EditText view, String oldText, String text) {
TextViewBindingAdapter.setText(view, text);
if (text == null) return;
if (text.equals(oldText) || oldText == null) {
view.setSelection(text.length());
}
}