--- _db_id: 596 available_flavours: - kotlin content_type: project prerequisites: hard: - projects/kotlin/project-5/live-data-observers soft: [] ready: true submission_type: repo title: Data binding with ViewModel and LiveData. --- ### App Intro ![](697975f88441847a.png) In this project, you improve the GuessTheWord app by integrating data binding with LiveData in ViewModel objects. This automates the communication between the views in the layout and the ViewModel objects, and it lets you simplify your code by using LiveData. ![Title screen](4abf85983013d4f3.png) ![Game screen](4abf85983013d4f3.png) ![Score screen](d6bf6cf728fb6c09.png) ### Task: Get started In this task, you locate and run your starter code for the previous project. You can use the GuessTheWord app that you built in previous project as your starter code, or you can download a starter app. - Run the app and play the game. - Notice that the Got It button shows the next word and increases the score by one while the Skip button displays the next word and decreases the score by one. The End Game button ends the game. - Cycle through all the words, and notice that the app navigates automatically to the score screen. ### Task: Add ViewModel data binding In a previous project, you used data binding as a type-safe way to access the views in the GuessTheWord app. But the real power of data binding is in doing what the name suggests: binding data directly to the view objects in your app. Current app architecture In your app, the views are defined in the XML layout, and the data for those views is held in ViewModel objects. Between each view and its corresponding ViewModel is a UI controller, which acts as a relay between them. ![](3f68038d95411119.png) For example: - The Got It button is defined as a Button view in the `game_fragment.xml` layout file. - When the user taps the Got It button, a click listener in the GameFragment fragment calls the corresponding click listener in GameViewModel. - The score is updated in the GameViewModel. The Button view and the GameViewModel don't communicate directly—they need the click listener that's in the GameFragment. #### ViewModel passed into the data binding It would be simpler if the views in the layout communicated directly with the data in the ViewModel objects, without relying on UI controllers as intermediaries. ![](7f26738df2266dd6.png) ViewModel objects hold all the UI data in the GuessTheWord app. By passing ViewModel objects into the data binding, you can automate some of the communication between the views and the ViewModel objects. In this task, you associate the GameViewModel and ScoreViewModel classes with their corresponding XML layouts. You also set up listener bindings to handle click events. #### Step 1: Add data binding for the GameViewModel In this step, you associate GameViewModel with the corresponding layout file, game_fragment.xml. - In the game_fragment.xml file, add a data-binding variable of the type GameViewModel. If you have errors in Android Studio, clean and rebuild the project. ``` Similarly, bind the click event of the correct_button to the onCorrect() method in the GameViewModel.