/ViewModelsDemo

A small demonstration application for showing the power of view models and data binding

Primary LanguageJava

ViewModelsDemo

A small demonstration application for showing the power of view models and data binding

Data binding with View Models

Here's an example of binding things with view models. Assume you have a text view, edit text. What we'll achieve is, when you type something, the text view will display the text taken from the edit text, simultaneously.

The main databinding is happening in your layout xml file, here & here.

In your view model, the same string is actually a mutable Live data of type String. Which basically means that the String value can be added to that live data and also be read from that live data. Here we are initializing it.

The setupBinding() function takes care of linking our view model, the activity and the layout together. The best part is that the these view models are aware of lifecycle of the activity so after typing, if you rotated the device for example, it wouldn't affect the user interface. It will persist that. This is the main reason of using view models.