Search code examples
androiddata-bindingandroid-databindingandroid-architecture-componentsandroid-livedata

EditText LiveData Two-way binding


Okay, so I have a ViewModel with a getter getTitle() that returns MutableLiveData<String>.

<EditText
    style="@style/Widget.EditText.FullWidth"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/label_title"
    android:inputType="text"
    android:text="@={ viewModel.title }" />

This works fine at first: the EditText contains the value of the MutableLiveData when it first appears. However, if the value of this data is updated using MutableLiveData.setValue() (such as by another EditText, or from my code), then the value inside of the text box does not change. How do I fix this?


Solution

  • This works properly in the new version of Android Studio, which supports binding to LiveData objects properly.