In the last post we examined some of the internals of the Android Data Binding Library.
In particular, we discovered that the current implementation of data binding is “one-way”. That is to say that if we bind a string value in a view model to an
EditText, a change in the
EditText value does not propagate back to the view model.
This might be unexpected for developers who have used data binding in other frameworks, like WPF or AngularJS. These frameworks support “two-way” binding out of the box.
So we’ll need to implement our own
EditText watcher to propagate changes from the view to the view model. What might that look like?
Watching the EditText
The easiest way to watch the
EditText is to implement a watcher.
Let’s create a SimpleTextWatcher that only requires only one method to be overridden: