2 Way Binding between ViewModel and xml Components

This is my note for Udemy lesson: Advanced Android Bootcamp 2024.

1. Enable xml to Access to ViewModel

see this note

2. Edit ViewModel

class MainActivityViewModel: ViewModel() {
  val userName = MutableLiveData("Frank")
}

3. In xml, When Display LiveData in ViewModel Only:

<TextView
  ...
  android:text="@{viewModel.userName}" />

4. In xml, When Display and Change LiveData in ViewModel:

<com.google.android.material.textfield.TextInputEditText
  ...
  android:text="@={viewModel.userName}" />