‫Hello developers, and welcome back.
‫Congratulations for your dealing with data binding library.
‫Now let's learn about a new concept and a little bit tricky concept called two way binding.
‫Imagine we have an edit text in our application.
‫Its ID is edit text, and the user wants to enter the name of the vehicle and to live update the text
‫view with the entered text.
‫To do this, we need to use two way binding, so any change in the edit text will be reflected to this
‫text view, and any change in the text view will be reflected to the edit text.
‫Let me show you guys this edit text will be linked with the variable called my vehicle that we created
‫before.
‫And inside this vehicle we have a property called named.
‫We need to access it and to update this, edit text with it and to update this text view.
‫Of course I want from you to fully focus with me because it's very important concept.
‫I need to use a property called text equals to the annotation of the data binding at curly braces and
‫my vehicle dot name.
‫But instead of using this, the two way binding ensures that you use the equal sign to be updated with
‫the newly entered data from the user.
‫We have an error here because I'm using this syntax.
‫This equal sign is a data binding expression that establishes a two way binding between the edit texts
‫text property and the vehicle name property of the vehicle class.
‫So here I am linking this edit text with the name of the vehicle object.
‫The equal sign indicates a two way binding, meaning change is to the edit text.
‫Text will update the name property in the vehicle automatically, and the changes to the name property
‫in the vehicle will update the edit text automatically.
‫This is the two way binding.
‫The text view will affect the text view and the text view will affect the edit text.
‫So with this binding, any changes made to the text in the edit text will automatically update the name
‫of the vehicle property and vice versa.
‫But we have an error here.
‫The expression it cannot be inverted, so it can't be used in a two way binding.
‫In order to use two way binding, we need to introduce the base observable implementation in the vehicle
‫class.
‫Since I'm using my vehicle and my vehicle, here is a vehicle class, I need to extend it from base
‫observable to base observable class here Alt plus enter to change the constructor invocation and here
‫we go.
‫Base observable is a class provided by the Android Data Binding library.
‫It's part of the Android architecture components and serves as a foundation for implementing the observer
‫pattern.
‫In the context of data binding.
‫The primary purpose of base observable is to make it easier to notify observers, such as UI components
‫when the underlying data changes, facilitating automatic updates to the UI, and I'll change the data
‫class to a normal class in order to use these variables with the Bindable annotation for the property
‫change, I'll remove this and I'll create here var model year.
‫It's of type string and var equals to empty string and var name.
‫It's of type string equals to empty string.
‫And here I need to use the Bindable annotation for the property.
‫Just follow me and I'll let your everything use add Bindable annotation.
‫We have an error here telling us to use data binding annotations in Kotlin.
‫Apply the Kotlin Kapt plug in your models plugin Build.gradle file.
‫Either I can use get like this, or I can change and the Build.gradle file and add the Kapt plugin to
‫our plugins in the Build.gradle dot css file, add the level of app.
‫So if we go back we can see I'm using the Bindable annotation.
‫But what is Bindable annotation?
‫The Bindable annotation is used in the context of Android data binding to.
‫Properties for which the data binding library should generate code to handle change notifications.
‫When a property annotated with a Bindable annotation changes, it triggers automatic updates in the
‫UI that is bound to the property.
‫This annotation is typically used in classes that extend base observable, so whenever the name changes,
‫it will trigger automatic updates in the UI that is bound to that property.
‫So it will notify this edit text that it is bound to.
‫And by the way, you can see the error is gone because we used the base observable and we used this
‫and annotate the name with Bindable annotation.
‫Again guys, when you mark a property in your data model class, for example, the vehicle class with
‫add Bindable annotation, you are indicating the data binding library that this property is observable.
‫So any change in this value, you need to update the UI that it is bound to that property.
‫Then I'll use here the set method.
‫And I'm going to modify the default set method because I need to use a method called notify property
‫changed.
‫So field equals to value.
‫This is the default thing that we learned about it in the setters and getters in Kotlin.
‫And I need to use a method called notify property.
‫Changed BR dot dot vehicle.
‫The notify property changed method is typically used in a class that extends base observable within
‫the context of the Android data binding.
‫So this method is part of the base observable class.
‫It's responsible for notifying any registered observers, such as UI components, that a property has
‫changed, and this notify property change will notify this edit text for any change.
‫The BR class is automatically generated by the data binding library, and it contains identifiers for
‫all properties marked with at Bindable.
‫Annotation in your project BR.
‫My vehicle is an example uh, identifier for a property named My Vehicle, and you can use this identifier
‫to specify which property has changed when calling notify property changed.
‫So for example, let me run and there's nothing to do in the main activity because we set everything
‫correctly.
‫And here I can remove v1.
‫And I can set it to like this dot name because I changed the primary constructor.
‫And here I name it as Mercedes and v1 dot modal year equals to 2024.
‫Let me run and you can see this is our application.
‫If you run it you can see that the edit text is set it exactly as the text view which is the property
‫of the vehicle class.
‫So if I change the from Mercedes to BMW and hit enter, but it's not updating the text view and the
‫error will be here in the Bindable I'm using BR dot my vehicle.
‫I need to use br dot name because I'm going to get this property and bind it to the edit text.
‫Let me run again.
‫Here we go.
‫If I remove the text, you can see the edit text is updated and it will automatically update this text
‫view.
‫If I type BMW and other text you can see the edit text.
‫Here is live updating the text view here.