‫Welcome back.
‫In this video we're going to add the data binding library to our project.
‫And this is a very important step because we're going to add data binding with Recycler View.
‫We talked about this topic in the previous sections.
‫And we implemented data binding with Recycler View in the retrofit and mVVM section.
‫But I repeat this because it's a very important concept and you'll use it for future projects and in
‫many apps.
‫So the first step is to enable data binding.
‫Let's go to the Android scope here and allow data binding by using build features.
‫And here I'll add data binding equals to true hit sync.
‫Now after that let's add the item card here.
‫Inside this layout I'll add a data variable.
‫And before data variable I need to include this inside the layout tag.
‫So layout add the closing tag for the layout and make sure you move the namespace declarations to the
‫layout.
‫It's better.
‫And move this to here okay, don't add any width or height attributes to the layout tag.
‫Okay, to prevent any possible errors, I'll add data and inside it I'll create a variable of type user.
‫I name it as user and of type master code and Firebase dot user class.
‫The first text view will display inside it at user dot name.
‫The second text view will display the phone number.
‫So at user dot phone number and the third text view will display the group.
‫So at user dot group it's very simple.
‫And we learned about it in the previous videos.
‫The third step is to move to the model class here and add the base observable.
‫Add Bindable annotation and notify data set changed method.
‫So again guys let's start with the base observable.
‫Here I need to extend base observable.
‫Base observable is a class provided by Android's data binding library.
‫And you can see it.
‫We imported it from the package Android x dot data bind.
‫It serves as a base class for implementing observable properties and objects that notify changes to
‫their properties.
‫You can extend Base Observable in your ViewModel or model classes, and annotate getter methods with
‫add Bindable to create observable properties.
‫When a property within a base observable object changes, you can call notify property changed method
‫to notify the data binding framework about the change which triggers UI updates.
‫We talked about this in the previous videos, so please guys, if you have any doubt, any question,
‫please go back to the previous sections or comment in the Q&A sections and I will help you to understand
‫it better.
‫The second step is to annotate the getter methods with the add Bindable annotation.
‫So we have three fields.
‫So we have three getters and three setters.
‫Let's start with the user name and annotate it with add Bindable.
‫Add Bindable annotation is an annotation used in Android data binding library.
‫It's applied to a getter method of an observable property in a data binding aware class, usually a
‫ViewModel or a data class, to indicate that the property can be bound to a UI element.
‫So this is telling that this user name can be bound to a UI element.
‫When the value of add Bindable property changes, it triggers a data binding update, causing associated
‫UI elements to automatically reflect the updated value.
‫It simplifies the process of keeping UI elements synchronized with data changes, reducing the need
‫of manual UI updates.
‫So when you add Bindable annotation, this get user name will notify this text view, which is using
‫user dot username with any changes that will happen again.
‫Guys, I'll go to the getter of the phone number and the getter of the user group.
‫The third step is to add the notify data set.
‫Changed method.
‫Here I'll go to the set username and notify property.
‫Changed is a method call used to notify the data binding framework that the value of an observable property
‫has changed.
‫This method is typically used within classes that extend base observable to trigger the updates in the
‫UI and in the views when a property's value changes.
‫Again guys, here we use the bind double and the last step is to notify property change.
‫And here I'll start with b r which refers for binding resources.
‫And this is an automatically generated class provided by the data binding library.
‫It contains integer constants that represents the Bindable annotation properties in your project.
‫The constants are named after the properties name.
‫In uppercase dot, you can see we have access to the user group, user username, all phone number and
‫class.
‫We are interested in the phone number, user name and group user.
‫I need to notify any change in the username, so I'll notify b r dot username.
‫You can see this is at bindable.
‫So I'm not defining any change here.
‫With this bind double annotated get username method.
‫In the same way I'll use it in the set phone number.
‫And here I'll notify the phone number.
‫And let's go down to the group user and notify the group user.
‫When you call notify property changed method, you are essentially telling data binding to update any
‫UI elements that are bound to the specified properties.
‫And this case we are calling it in three times.
‫So the three text views here are updated with the latest changes for the values.
‫User name, phone number and group user data binding will take care of refreshing the UI elements associated
‫with that property, ensuring that display or that they display the updated value.
‫This is how we add data binding to the layout and the modal class.
‫Now let's learn how to add the data binding to the adapter class.