‫Welcome back.
‫Let's add the data binding here.
‫Inside the build.gradle js file I added the build features and enabled data binding in the previous
‫videos.
‫Also I have added this layout tag and I created this variable of type counter ViewModel.
‫Let's scroll down to the text view and here I'll use this text attribute and add the syntax of data
‫binding to get from my view model the counter.
‫But you can see I can't access the counter because it's private.
‫So remove the private.
‫Or you can use a getters and setters and create a getter method for this counter.
‫But I prefer for for demonstration I need to use the public.
‫So this is our text view.
‫It will receive the live data from this view model.
‫Also I need to add here for this button the onclick attribute and add the syntax.
‫My view model increment counter.
‫This error is solved by passing a view parameter here of type view okay and import the class of the
‫view.
‫And here we go.
‫So always when you need to access a function in data binding, you need to use the and pass a view parameter
‫inside its parameters.
‫Now let's run our application.
‫And maybe I missed this.
‫To add it binding object dot.
‫My view model equals to counter view model.
‫I am linking this object with the view model variable inside this layout.
‫And here I have an error.
‫Let me show you the Logcat.
‫You can see it's caused by text view dot set text.
‫The problem here is I'm getting direct integer into the text view.
‫So here I can solve it by using dot toString method.
‫Let me run again and here we go.
‫Increase the counter.
‫If I scroll down and rotate the screen, it's increased by ten and you can see if I increase the counter.
‫The text view is not updated until I make a configuration change.
‫You can see 16.
‫So this is due to the problem that I missed.
‫To add the observe method here I should add binding and specify the lifecycle owner equals to this run.
‫Again, increase the counter and here we go.
‫Our application is working fine and everything is good.
‫When you are using the data binding in Android, you often have a binding object associated with your
‫layout, and it's used to reference views from your layout in your code.
‫The lifecycle owner property of the binding is particularly irrelevant when you are dealing with live
‫data or other observables inside your layout, so whenever you use and and deal with live data, you
‫should add lifecycle owner.
‫This is how we add lifecycle Owner and set the live data properly in Android.
‫Also in this example we used the view models, the live data and the data binding.
‫Congratulations guys.
‫Now let's move to create new and complex applications.