Complete Guide to Android Data Binding Library – Part -1

What is Android Data Binding?

Android Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.With Data Binding we can use our XML UI code itself to make dynamic UI.

For Example, Let say we want to change TextView’s text. How we will achieve this on without using Data Binding & Using Data Binding Library.

Without Android Data Binding:

With Android Data Binding:

The Data Binding Library offers both flexibility 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.

How to use Android Data Binding Library?

To add Android Data Binding in your Android Project, you need to add an element dataBinding to your build.gradle file in the app module, as shown in the following example:

Above mentioned lines will enable Data Binding Environment to your project. Now come to the point how we will use Data Binding. While creating UI for Android app we create some XML/ Layout file which is slightly different when we use Android data binding. As mentioned below:

Layout file without Data Binding:

Layout file with Data Binding:

We need to clearly understand that without adding a taglayout element to UI XML we cannot achieve Data Binding. When we add a taglayout to view layout file and save than a View Binding class being generated automatically with layout file name pattern.  For example, our layout file name is activity_main.xml  then the binding class name will be like MainActivityBinding to bind views.

It was fine until ViewBinding class creation, Now

Setting Data to View components dynamically:

The binding variables that can be used in expressions are defined inside data element that is a sibling of the UI layout’s root element. Both elements are wrapped in a layout tag, as shown in the following example:

The user variable within data describes a property that may be used within this layout.

Expressions within the layout are written in the attribute properties using the “@{}” syntax. Here, the TextView text is set to the firstName property of the user variable:

Data Object:

From the perspective of data binding, these two classes are equivalent. The expression @{user.firstName} used for the android:text attribute accesses the firstName field in the former class and the getFirstName() method in the latter class. Alternatively, it is also resolved to firstName() if that method exists.

Now point comes, How we will bind this User data object to ViewBinding.

At runtime, the app displays the Test user in the UI. Alternatively, you can get the view using a LayoutInfater, as shown in the following example:

If you are using data binding items inside a FragmentListView, or RecyclerView adapter, you may prefer to use theinflate() methods of the bindings classes or the DataBindingUtil class, as shown in the following code example:

Now we are gone through Android Data Binding Basics.

Expression Language in Android Data Binding:

Expression Language in Data Binding Android Basically refers to Operators and Data Binding Expressions used with Android UI components (XML). There are some basic expressions are mentioned below:

Common operator & Feature:

There are some operators & features mentioned below which we can use for basic operations in XML (UI Components)

  • Mathematical + – / * %
  • String concatenation +
  • Logical && ||
  • Binary & | ^
  • Unary + – ! ~
  • Shift >> >>> <<
  • Comparison == > < >= <=
  • instanceof
  • Grouping ()
  • Literals – character, String, numeric, null
  • Cast
  • Method calls
  • Field access
  • Array access []
  • Ternary operator ?:

For Example:

Keywords which we can not use:

There are some java keywords mentioned below which we can not use with Data Binding Expressions till now.

  • this
  • super
  • new
  • Explicit generic invocation
Null coalescing operator:

The null coalescing operator (??) chooses the left operand if it isn’t null or the right if the former is null.

which is functionally working as

Property Reference:

An expression can reference a property in a class by using the following format, which is the same for fields, getters, andObservableField objects:

Collections in Data binding & Avoiding null:

We can use collections such as arrays, lists, sparse lists, and maps, can be accessed using the [] operator in Data Binding. We can import things like mentioned below:

And Now its time to use collections

Using Android Resources:

We can use resources with UI Components, Example mentioned below

Some resources require explicit type evaluation, as shown in the following table:

Type Normal reference Expression reference
String[] @array @stringArray
int[] @array @intArray
TypedArray @array @typedArray
Animator @animator @animator
StateListAnimator @animator @stateListAnimator
color int @color @color
ColorStateList @color @colorStateList

Event Handling in Android Data Binding:

In Data Binding Expressions we can handle events as well. There are mostly two ways one is Method Reference other is Listner Binding. We can use both the ways as per convenience. Below, I’ve mentioned a Java class which will cover both the ways.

In above-mentioned code, there are three methods. Now check with Data Binding expressions mentioned below:

Avoid Complex listeners, to simplify things in manner.

Variables, Imports, Type aliases & Includes in Data Binding:

You can use multiple variable elements inside the data element. Each variable element describes a property that may be set on the layout to be used in binding expressions within the layout file. The following example declares the userimage, and note variables:

Imported types can be used as type references in variables and expressions & When there are class name conflicts, one of the classes may be renamed to an alias.

Variables may be passed into an included layout’s binding from the containing layout by using the app namespace and the variable name in an attribute. The following example shows included user variables from the name.xml andcontact.xml layout files:

Hope this post helped you to have Understanding Fundamentals of Android Data Binding Library.

I’m Dipendra, software developer, and designer who has written code in many of the language known to mankind.Passionate about Technology. In my free time, I love to write articles and build the code.

Leave a reply:

Your email address will not be published.

Site Footer