‫Welcome back.
‫In the previous video we created this layouts activity underscore main and move list item.
‫In this video, we'll use the data binding library that we implemented in our Build.gradle file and
‫enable it using build features data binding equal to true to associate these widgets in the layout with
‫their corresponding data variables in the main activity.
‫I'm not going to create any variable in the layout of the movie list item.
‫I'll create a new data variable.
‫And I'll name it as movie.
‫And the type of this data variable is a movie class that we created before.
‫So the movie app and here I select modal and select this movie class.
‫Let's go to the movie class again, inside the modal package we have one, two, three, four, five,
‫six, six variables that we can associate with which I want from you to focus with me.
‫I need to display an image here.
‫I need to display the rating and I need to display the title of the movie.
‫For that, I'll start with the text and later on I'll go back to the image.
‫So skip image view.
‫Now, in this text view, I'll use the syntax of data binding that it is associated with the text equals
‫to at and the syntax of data binding movie dot and I can get all of these variables.
‫The ID overview poster path, release date, title and vote average.
‫I'll use the title so this is for the title and the second text view is for the rating.
‫So for that I'll use text the same as before.
‫And here I'll use movie dot vote average.
‫But there is a problem here.
‫Why I can't use movie dot average data binding in the text views doesn't accept double variables.
‫So if we go to the movie we can see it's of type double data binding only accept string variables.
‫And I mentioned this very important note in the previous videos about data binding.
‫What is the solution?
‫I can't change this double because it's of type double in the vote average response of the Json response.
‫I need to include this in a method double dot to a string and this will transform a double into a string.
‫Okay.
‫So I'm converting a double into a string in order to display it inside a text view using the data binding.
‫This is a very important note.
‫You should pay attention to it.
‫So before I move to the image view, I need to set the title and the vote average as Bindesboll's.
‫I'll scroll up to allow the class this movie class to extend from base observable and I am importing
‫this from data binding library.
‫When you extend base observable class in your modal or data class, you enable automatic data binding
‫and make it easier to bind data between your data objects and the components in your apps layout file.
‫So in order to tell Android studio that this text view is linked to this title variable, all I need
‫to implement or extend the base observable and I need to tell Android studio that this method get title
‫is a bindable method.
‫These annotations add Bindable signal to data binding that these properties can be bound to the UI component.
‫So this is the get title and the get vote average will be also bindable.
‫Whenever you need to bind any property that the UI components in the layout, you should extend the
‫base observable and add the add Bindable annotation to that property.
‫By extending base observable and using data binding, you can establish a robust and efficient data
‫binding mechanism that automates the synchronization of your data objects with your UI, reducing the
‫amount of boilerplate code required to manage these updates manually.
‫And one last thing you need to mention is the notify property changed in every set method.
‫So whenever you use data binding, you should extend the base observable and the bindable for the getters
‫and add the notify property changed to the setters.
‫So notify property change method and here I'm going to specify VR dot title and for the vote average
‫I need to specify or use notify property changed VR dot vote average Notify property change is a method
‫provided by base observable class that it is used in conjunction with the Android data binding library
‫to notify the data binding framework that a property in a data object has changed and BR stands for
‫Binding Resources.
‫It's an automatically generated class by the Android data binding library.
‫And it contains integer constants that represent the binding expressions used in your layout files.
‫The title and the vote average refers to the properties you want to notify.
‫Data binding about.
‫As a result, any UI elements bound to this property will automatically update to reflect the new value.
‫So this is how we use data binding in the movie data class.