‫Welcome back.
‫In this video I'm going to continue with data binding Library.
‫And I'm going to use data binding in Android to set the content view of an activity and bind an object
‫to the layout.
‫So the main purpose of this video is to bind objects to the layout.
‫I want from you to focus with me here.
‫I'll create a new data class and I'll name it as a vehicle.
‫This vehicle class takes two arguments in its constructor val model year.
‫It's of type string and val name.
‫It's of type string.
‫And let's go back to the main activity.
‫And here I'm going to create a new object of this class and access one of these attributes I'll create
‫var vehicle one equals to vehicle.
‫And here set the model year and the string here the model year is 2024.
‫And the name Mercedes I'm going to get rid of this and set V1 dot I'm accessing the name okay.
‫If I run the project now.
‫And here we go.
‫This application is reading from this object and setting the text of the text view for the name attribute
‫of the v1 object.
‫But there is a very important concept here that I should mention.
‫Using data binding, I can bind this object to the layout directly without interfering with text view
‫programmatically.
‫What I mean with this.
‫First of all, to bind objects to your layout in data binding, I need to create a data field.
‫Here in the layout, I'll start under the layout and before any view, any widget, any layout, I should
‫start with the data field here.
‫Data and the closing tag of the data and I'll start declaring a variable.
‫This is a key part of the Android data binding library, allowing you to associate data with your layout.
‫The data section is a special example block of code in your layout file used for data binding.
‫It allows you to declare variables that can be bound to your data model in the associated activity,
‫or fragment the variable element inside the data section.
‫You use variable element to declare a variable that will be used in your layout, and in this case,
‫I'll name my variable my vehicle and the type attribute specifies the fully qualified class name, including
‫package of the type of the data that this variable represents, and in this case I start with com master
‫coding dot data binding app dot.
‫What is the type of the variable?
‫It's of type vehicle class.
‫I am referring for this class inside this package.
‫So you need to specify the whole package and then specify the name of the class again.
‫Guys the type of this variable is com dot master coding dot data binding app dot vehicle I'm referring
‫for this class.
‫The name of the variable is my vehicle.
‫The type is vehicle.
‫By declaring this variable in the layout, you are telling the data binding library that you expect
‫to bind an object of type com dot master coding, dot data binding app dot vehicle to the layout and
‫you'll reference this object as my vehicle within the layout.
‫Here I'm going down to the text view and instead of using text directly, I'm going to use a new annotation
‫that it is specific for data binding which is at curly braces.
‫And I'll access the variable, my vehicle and what we need to get from this my vehicle I need to get
‫the name.
‫And you can access the model here, but I'm going to access the name.
‫You can see this annotation is specific for the data binding.
‫I'll start with at sign curly braces.
‫Specify the variable name and the attribute.
‫And you notice that nothing happened here because you need to run the app at first to generate the class
‫of data binding and to access my vehicle dot name attribute and set it to this text.
‫Back to the main activity.
‫Now I can get rid of this binding dot TextView dot text.
‫But there is a very important note here that I should mention it.
‫I need to use and.
‫Enact this variable in Kotlin file with a variable that we created in the Xaml file, and to link this
‫variable with this variable that we created in KTX files and Kotlin files, I need to use binding dot
‫vehicle.
‫My vehicle equals to v1.
‫So this variable that we created now is linked to this variable in the layout file.
‫Let's run our application.
‫And congratulations guys.
‫We're getting the name from v1 associating this variable to this variable in the layout.
‫So I am linking this object v1 to this variable in layout.
‫So any change here in the name of v1 will be automatically reflected in the UI.
‫Updates and changes.
‫If I change it to BMW, run the application and you can see that it is directly reflected without the
‫need to set for the new text.
‫So here you can see guys that I'm no longer use the find view by ID method, and I'm no longer using
‫the binding dot text view dot text attribute.
‫I am connecting the vehicle variable to this vehicle variable in the layout.
‫This is the power of data binding and don't miss.
‫In the next videos, I'm going to show you more and more about the data binding features.