Android Jetpack:Empower your UI with Android Data Binding

Temidayo Adefioye
Jun 11, 2018 · 4 min read

Data Binding Library is a support library that enables you to bind UI elements in your layouts to data sources in your app using a declarative style rather than programmatically.

Often times, layouts are declared in activities with code that calls UI framework methods. For instance, the code below calls findViewById() to look-up for a TextView widget and bind it to the email property of the viewModel variable:

In the previous article, I discussed extensively on how to accelerate development and eliminate boilerplate code with Android Jetpack. Data Binding is one of the components of Android Jetpack which allows developers to declaratively bind observable data to UI elements. Hence, it removes many UI framework calls in your activities, making them simpler and easier to maintain.

Let’s take a look at a basic Data binding call.

The awesome part of data binding is that with just a single line of code, I can bind a username to a textview.

Getting started with DataBinding

The Data Binding Library offers both pliability and broad compatibility — it’s a support library, so you can use it with devices running Android 4.0 (API level 14) or higher.

It’s recommended to use the latest Android Plugin for Gradle in your project. However, data binding is supported on version 1.5.0 and higher

To start using data binding on your android studio:

  1. Download the library from the Support Repository in the Android SDK manager.
  2. Configure your app to use data binding, add the dataBinding element to your build.gradle file in the app module, as shown in the following example:

Android Studio support for data binding

Android Studio supports several of the editing features for data binding code. For instance, it supports the following features for data binding expressions:

  • Syntax highlighting
  • References, including navigation
  • XML code completion
  • Flagging of expression language syntax errors

The Preview pane in Layout Editor shows the default value of data binding expressions if provided. For example, the Preview pane displays the default_value value on the TextView widget declared in the following example:

If you need to display a default value only during the design phase of your app development, you can use toolsattributes instead of default expression values.

Layouts and binding expressions

The expression language provides developers with expressions that handle events dispatched by the views. The Data Binding Library automatically generates the classes necessary to bind the views in the layout with data objects in your project.

As for data binding layout files, these are slightly different and start with a root tag of layout followed by a dataelement and a view root element. The view element is what your root would be in a non-binding layout file:

The person variable declared within data describes a property that may be used within this layout. Also, if you want to use expressions inside your layout, you can call attribute properties using the “@{}" syntax.

In the example below, the TextView text is set to the firstName property of the person variable:

Let’s try to bind a mock-up data to our existing layout.

Before we do that, you need to take a look at the Person object

By default, a binding class is generated for each layout file. The name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it. The above layout filename is activity_main.xml so the equivalent generated class isMainActivityBinding. This class holds the entire bindings from the layout properties (for instance, the person variable) to the layout's views and knows how to assign values for the binding expressions. The best practice method to create your bindings is to do it while inflating the layout, as shown in the following sample:

You can get the view using a LayoutInflater

If you are using data binding items inside a Fragment, ListView, or RecyclerView adapter, you may choose to useinflate() methods of the bindings classes or the DataBindingUtil class

Cool bananas!!

In the next article, we will dive into the heart of Data binding and in the end, you will be able to comfortably use this component of Android Jetpack for more complex UI.

Temidayo Adefioye

Written by

Technical Writer | Google Certified Associate Android Developer | Open Source Advocate | Android Subject Matter Expert @Pluralsight| I love clean code!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade