‫Data binding is built around the idea of using data from a regular Java or Kotlin object to set attributes
‫on your layouts, and that's the extent to which most people use it.
‫This is great because it allows you to define your view logic independently from the Android framework.
‫But what if the your view needs to set attributes on your object?
‫For example, if I have an edit text and I want to get the data from this edit text how I can use it
‫with the data binding library.
‫This is where two way data binding comes in and this is what we'll learn in this video.
‫Two way data binding is a technique of binding your objects to your XML layouts so that the layout can
‫send data to your binding object.
‫This is compared to a traditional or one way data binding setup that we learned before where data would
‫only move from your binding object to their layout.
‫So to better understand two way binding, I'll create a new edit text and one text view.
‫I'll drag and drop it here and infer the constraints.
‫Then I'll create an edit text.
‫I'll drag and drop it to here and infer the constraints I want from you to focus with me in order to
‫understand how to link this edit text with the text view in two way binding technique.
‫So this technique two way binding allows you to automatically update UI elements when the underlying
‫data changes and vice versa.
‫For example, if the user enters his name here, this text view will be changed automatically, ensuring
‫that the changes made by the user in the UI are reflected back in the data source.
‫Without writing explicit code for these updates, let's assume that we need to enter the person name
‫here.
‫So I'll use this variable called person that we've created in the data element before, and I set it
‫for both the text view and the edit text.
‫So here I need to use at curly braces person dot name.
‫Also, I need to set this edit text and start with text at person variable dot name.
‫I need to display the name here and the name here.
‫The second step is to move to the data source, which is in this case the person class in the data binding
‫two way data binding technique.
‫I need to extend base observable class.
‫Extending base observable is not strictly required, but it can be useful to implement automatic property
‫change notifications, especially when using add Bindable annotation and custom getters and setters
‫for properties in your ViewModel or data source.
‫And in this case the data source is the person class and extending base observable provides the notify
‫property change method which allows you to notify the data binding library when a property has changed.
‫This is essential for the library to automatically update UI elements when the underlying data changes.
‫For that, I'll use this Notify property changed method in the Setname method notify property change
‫and I need to use B-R.
‫This is from data binding library dot name.
‫So the notify property changed method is used in Android's data binding library to notify the library
‫that a specific property has changed its value.
‫The notification is crucial for two way data binding and as it informs the library to update any UI
‫elements bound to that property.
‫And this method is provided by base observable.
‫So we extend base observable to gain access to this method B stands for binding resources and it's a
‫generated class by the data binding library.
‫This class contains constant integer values associated with each of the bindable properties in your
‫data source.
‫In other words, there is a constant for each property you've marked as bindable and you can see it
‫can't find name because if I go up I can't see any bindable annotation.
‫In order to solve this error, I'll add at bindable annotation here before the getName method, the
‫Bindable annotation is used to mark the user name here property observable, which tells data binding
‫to generate the necessary code to observe changes.
‫To summarize what we've done before, I created this data source person class extending base observable
‫to gain access to notify property changed method and this method I am passing here be are binding resources
‫dot name and this name refers to any bindable property, and in this case I'm telling data binding that
‫the name property has changed its value and telling data binding to generate the necessary code to observe
‫changes.
‫Now let's move to the main activity and here I need to set the two way binding.
‫But since we've done it before, I need to create an activity main binding and initialize it and activity
‫main binding dot set person to p one.
‫I'm setting the person jack at first and I can edit this edit text and display the changes into the
‫text view.
‫Let's run.
‫So no need to write anything here and you can see I have this text view, the old text view.
‫I have this new text view called Jack and this is the edit text.
‫This is Jack.
‫I need to change it and type, for example, John And you can see there is an error.
‫John Value is not being updated, so Jack still persists.
‫This is an error many developers make.
‫So there is a very important trick here in the syntax of the edit text.
‫So this equal sign will solve the problem.
‫In Android's data binding library, the app equals sign curly brackets syntax is used to enable two
‫way data binding.
‫It allows for bidirectional data flow between a UI element and the data source, usually a ViewModel.
‫Later on we'll talk about Viewmodels with two way data binding changes made to the UI element automatically
‫update the data source and the changes made to the data source.
‫Automatically update the UI element so don't miss to use this syntax when dealing with the edit texts
‫run and you can see if I enters John, you can see that it's automatically updating the text view.
‫Even if I remove the text, you can see I'm updating this edit text and in turn update the text view
‫and this other text view.
‫This is the two way binding in Android.