Android data binding — 3
EditText & Two-way data binding
Basically, Android data binding does not support two-way data binding yet (hope will be soon). To handle two-way data binding, we need some manual steps.
One-way auto UI update
First, about Observable object. If you wanna UI task listens and updated when view-model data changed, think about observable object
In this example, we have one TextView & Button. When user click on Button, TextView show click counter

Take a look at activity_main.xml

①: Bind TextView text to viewModel.text
②: Bind button click event to viewModel.onClick
For View Model:

①: make ViewModel now “observable”
②: Mark “text” now bindable. This annotation then auto-generates BR.text, used in setText
③: when text is changed, notify that “text” now changed, then UI will be auto-updated
Now when user clicks on Button, increase counter & also update “text” property — then bound TextView will be updated too

Two-way binding with EditText
Android now does not support officially two-way binding, however we can do it manually. For EditText, we map text value to prefix ① (optional) and we listen if text change ②

And ViewModel we add prefix property ① and TextWatcher ②

When text is changed, we update prefix and notify text changed
