DEV Community

Cover image for Android Jetpack: DataBinding Library
Paul Franco
Paul Franco

Posted on

Android Jetpack: DataBinding Library

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

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

The next step is to generate a binding class for the XML layout. To do this we have to wrap the XML layout with tags.
Alt Text

We then have to move the xmlns lines from the constraint layout into the opening tag.
Alt Text
Alt Text

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
Alt Text

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
Alt Text

To use the Data Binding object and simplify our code we have to declare a reference variable for it in our MainActivity.
Alt Text

Since we are now using Data Binding we no longer need the setContentView() function in our onCreate() method.
Alt Text

We will instead initialize the Data Binding object with DataBindingUti.setContentView().
Alt Text

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

We can now use the Binding object. Instead of button.setOnClickListerner. we will use binding.submitButton.setOnClickListener.
Alt Text

We can now do the same for the other view references in the displayGreeting() function.
Alt Text

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

Latest comments (0)