Last Updated:

Data Binding in Android - Get rid of template code

Data Binding in Android

We continue to get acquainted with technologies that allow you to speed up and simplify the process of developing applications for Android. In this article, the Data Binding library will help us get rid of templated and optionally code.

Introduction to Data Binding in Android

 

Calling to access the View is unnecessarily cumbersome, especially in large projects with complex layouts and logic. In such cases, the template part, which has nothing to do with the logic of the application, grows by dozens, or even hundreds of lines.findViewById

To simplify the development process and reduce the number of lines of code, third-party libraries have been created, for example, ButterKnife – read the review here. It uses annotations , , , , etc. to replace boilerplate code. The library does a great job, but still requires defining types and specifying identifiers for each view, even in one line.

@BindView@BindString@BindDrawable@OnClick

Integrating Data Binding into an Android Studio Project

 

Completely get rid of this will help us Data Binding in Android. Let's create a new project in Android Studio and enable the ability to use data binding in our project script. To do this, add the following line in the block:build.gradleandroid

Now synchronize the project to complete the integration as the Android Studio development environment wants.

Now we need to make the necessary changes to the layout. Now it looks like this:

activity_main.xml

 

We have to put our layout in the root :<layout>наш_макет</layout>

Notice that we've also added a required id attribute for the TextView. Now let's go to the file and implement the data binding in the code:MainActivity.java

In my case, the ActivityMainBinding was not created when I tried to synchronize the project, so I simply rebuilt the project after adding the code above (Build -> Rebuild Project).

A completely logical question arises: how did the class appear, because we did not describe it anywhere? There's nothing magical here: the class is generated by the Data Binding library when the development environment rebuilt the application.

ActivityMainBindingActivityMainBinding.java

Why ActivityMainBinding?

 

The class name comes from the name of the layout file (without the extension) using camel notation (this is CamelRegister, a style of writing in which words are capitalized without spaces) with the addition of "Binding". Thus, we got the following class name for data binding:activity_main.xmlCamelCase 

activity_main.xml -> activity_main + Binding = ActivityMainBinding

 

Also note the replacement of the familiar method with a proxy call to the binding library: . It initializes the generated class with the context of the current activity.

setContentView()DataBindingUtil.setContentView()

Now let's try to access our TextView inside the layout using its ID:

activity_main.xml

 

Convert id to variable name

 

In the example above, each identifier in the XML file is automatically converted to Java variables. In fact, it all comes down to a simple replacement of underscores in CamelCase, but there are several nuances in such a transformation:@+id/

If we also had an identifier, then the binding library would only see . Therefore, do not use different naming styles for identifiers, or it can cause additional complexities and problems during development.tv_hellotvHellotv_hello

Findings

 

The Data Binding library in Android has huge opportunities to speed up and simplify the development process, while reducing the amount of unnecessary code. This allows you to focus on creating logic rather than the behavior of the View in different situations.

With that, a brief overview of the linking library in Android comes to an end. In future articles, we'll talk about the more advanced capabilities of integrating Data Binding into an Android app.

 

Subscribe to new materials and share this article with your friends!