Android Jetpack: DataBinding Library
Tags: android, jetpack, databinding, tutorial
Published: 2020-09-04 15:57:21

Android Jetpack was introduced in 2018 by Google in order to help developers accelerate android development. Jetpack is a set of components, tools, libraries, and architectural guidance that makes it quick and easy to build great Android applications. One of these libraries is Data Binding.

The Data Binding Library is a support library that allows developers to bind UI elements in the layout to data sources in the application using a declarative format rather than doing so programmatically. Layouts are often defined in activities with code that calls UI framework methods. For example, the code below calls the findViewById() function to find a TextView and bind it to the messageView variable and the EditText to the nameText variable.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2w4a08jv8uaa8c4jiyxk.png)

Every time we use the findViewById() function to obtain references to views, the Android system has to go through the View hierarchy and find it at runtime. In a large application, this could be many layouts and hundreds of views. Currently, most of the Android phones in the market have a refresh frequency of at least 60 hertz meaning that the screen refreshes at least 60 times per second or approximately every 16 milliseconds. Some phones in the market have higher refresh frequencies giving the Android system even less time to recreate the views. Therefore, having to go through the view hierarchy, again and again, is not a great idea because it reduces the performance of large Android applications.

Data Binding creates a binding object that contains a reference to each view of a layout sparing the Android system from having to go through the view hierarchy, again and again, to find a reference to a view at runtime. Data Binding not only improves the performance of the app but eliminates the use of the findViewById() function making code easier to read and maintain.

The first thing we need to do to implement Data Binding in our Android application is to enable dataBinding in the buildFeatures block of the apps build.gradle file.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ahclicmhenwid5ciakqz.png)

The next step is to generate a binding class for the XML layout. To do this we have to wrap the XML layout with <layout> </layout> tags.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t656rufle9t854vzf08a.png)

We then have to move the xmlns lines from the constraint layout into the opening <layout> tag.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zh86t3md7vh1sqxb9aiv.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ine2crl4qle7poest0nq.png)

The Data Binding library will take the name of the layout, in this case, activity_main, and will create a Data Binding object with a similar name like ActivityMainBinding.

In our MainActivity file, we currently have a Button that uses the findViewById() function. We then set an onClickListener that calls the displayGreeting() function.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ttd5bqqwqgk5a9n151lm.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/84j7vx1yymqo9inxi07i.png)

In the displayGreeting() function we declare and initialize a variable named messageView of type TextView and we use the findViewById() to find the view reference. We do the same for variable nameText of type EditText. We then change the text of the messageView and append the text that the user enters in the nameText EditText of the application.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pwi2wbv2cijot6v7cvie.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wrnz0li1ij0cxp9feswy.png)

To use the Data Binding object and simplify our code we have to declare a reference variable for it in our MainActivity.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q2rdk64eabejn5n637d1.png)

Since we are now using Data Binding we no longer need the setContentView() function in our onCreate() method.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/klxs4jw2v19avd1d46bx.png)

We will instead initialize the Data Binding object with DataBindingUti.setContentView().
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/puj0ptiu23q5t77v6frd.png)

The Data Binding object contains properties for each of the views in the activity_main layout. It used id names for each of the views and created its own reference by getting rid of the underscore and camel casing the name. For example: The Button id in the XML layout is submit_button and the Data Binding object created its own reference with the name submitButton.

We no longer need the findViewById() functions and we can remove them.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nc36zb9cva76evh37kum.png)

We can now use the Binding object. Instead of button.setOnClickListerner. we will use binding.submitButton.setOnClickListener.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/azriwmm35n3bpyzghozt.png)

We can now do the same for the other view references in the displayGreeting() function.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dq3hndt6uqozf0rhhrbz.png)

As you can see, Data Binding allows us to write readable and maintainable code while improving the applications performance.