‫Let's create our data binding application.
‫Open Android studio, select a new project, empty Views Activity and name it as data binding app.
‫To get started, you need to set up your development environment within your app to use the data binding
‫library.
‫The data binding library is bundled with the Android Gradle plugin.
‫You don't need to declare any dependency on the library, but you must enable it for that.
‫I'll go to Gradle Scripts.
‫I'll select this app level and this is the scoop of Android tag.
‫Scroll down.
‫And to enable the data binding library.
‫I'll use build features and here data binding.
‫True.
‫So when setting the data binding build option to true in your models build.gradle file, you are enabling
‫data binding.
‫This is the first step.
‫Click on sync now and congratulations guys.
‫Now you're able to bind UI components in your layouts to data sources in your app using a declarative
‫form.
‫This is the first step enabling data binding library in Gradle.
‫The second step is to go to the layout.
‫We need to convert this standard layout into data binding layout, and this is done by using the layout
‫tag which will wrap your existing layout file at the root level.
‫Here.
‫At the root level, we have this constraint layout.
‫I'll use the layout tag with small letter and I'll move this closing tag to the bottom.
‫The layout element is the root element of the data binding layout file.
‫It specifies the top level structure of your layout similar to the regular relative layout, linear
‫layout, constraint, layout, or other layout elements you should use in XML and inside the opening
‫tag and the layout tag, we should specify the XML namespace for data binding.
‫So for that I'll copy these namespaces here and move them to the layout.
‫Like this.
‫This is the root element of our layout and typically include other UI elements within the layout element
‫like this constraint layout and this text view.
‫So till now we have a layout which is the root element.
‫We have this constraint layout and inside this constraint layout we have this text view.
‫So the root element is the layout tag.
‫The third step is to use the variable tag and element, and the variable element is used within the
‫data element to declare variables that will hold data You want to bind to your layout.
‫For that, I'll place the data element under this layout.
‫The first step is to add the data binding to the build.gradle and enable it.
‫The second step is to go to the layout and add the layout as a root element.
‫The third step is to declare a data element under this layout and before the other children.
‫I'll start with data element.
‫The data element is used to enclose all the data binding related elements in your layout file.
‫It defines the scope for variables and expressions that link UI elements to data.
‫Sources.
‫Place the data element as a child of layout element and inside the data element you can declare one
‫or more variable elements to associate with data source.
‫The fourth step is to declare the variable tags and variable elements inside the data element.
‫I'll use variable and you can see we have two attributes the name and type.
‫The variable element is used within the data element to declare variables that will hold data you want
‫to bind to your layout.
‫These variables are typically instances of classes or view models that provide the data for your user
‫interface.
‫Place the variable elements within the data element.
‫Declare a name for a variable using the name attribute for example person.
‫And here I need to specify the type of the data using the type attribute.
‫This is a very important step.
‫For that, I'll create a new class that will act as a data source.
‫I'll name it as person, and inside this person class I'll declare two variables one of type string
‫called name and the other of type string called email alt plus insert to create the constructor, having
‫the name and email and alt plus insert to create getters and setters for both name and email and I'll
‫create an empty constructor alt plus insert select none.
‫This is just for null safety reasons.
‫I need to get the name of.
‫And displayed inside this text review.
‫So for that I need to declare the type of data source that I need to display in this layout.
‫I need to use person class.
‫So for that com dot master coding dot data binding app dot person class and this is the hierarchy here.
‫Com dot master coding dot data binding app dot purse.
‫You should specify the type here for the variable in the data element.
‫I am telling Android data binding library that the type of this variable is a person class.
‫The fifth step is to bind the views with the variables of the data binding.
‫For example, this text I want to display the person name inside this text attribute that it is used
‫to display texts inside this text view I will start with at and two curly braces.
‫I want from you to focus with me now.
‫This is the syntax used for data binding expressions in Android.
‫Inside the curly braces you can specify the data source and the property you want to bind to the UI
‫element.
‫In this case, I have this one variable and only variable called person.
‫I specify here person.
‫This refers to the data source object.
‫So person refers to the data source object, and in this case it's assumed that there is an object named
‫person here.
‫Then we need to add dot and you can see I can access, set, email, set, name, email and name attributes.
‫I need to display the name at first.
‫This is the property of person object that you want to bind to the UI elements.
‫Text property.
‫You are extracting the value of the name property from the person object and displaying it in the UI
‫element.
‫Save the project and let's move to the main activity to create an object of the person and see the data
‫binding in action.