MindOrks
Published in

MindOrks

Data Binding for Android Beginners — Introduction

This is the first article in this series…

The Data Binding Library is a support library that belongs to the Android Jetpack ( collection of libraries that help you build more productive applications). It was released 2015 with the API Level 23 (Marshmallow) but its backward compatibility survives till API Level 7 (Eclair)

The main purpose of data binding is to minimize the way we play with our views in our activity. In this sense, all views manipulation should be handled in the layout files rather than the activity thus making your layout alive. This is to reduce multiple redundant codes and non-logical codes present by removing UI framework calls in the activities.

When you enable data binding in your application, for each layout properly created for data binding, a class is generated. This means that generated classes are different for each created layouts. The classes generated are known as binding class.

To enable data binding library in your application, you include the dataBinding closure (function bundled together with its lexical environment) and its property enabled = true in your app build.gradle.

Data binding requires Android studio 1.3.+

A layout is said to be properly created for data binding if the layout begins with a layout root tag followed by a view root element.

The generated class with ❤️

The generated class naming convention is quite easy. It picks the layout name in camel case and suffixes it with “Binding”. Remember my layout name is activity_main. So the generated class will be ActivityMainBinding.

If you want to change the name of your generated binding class, you declare the class attribute on the data itself and specify the class name. In this case, I changed mine to MainActivityBinding.

layout xmlns:bind="http://schemas.android.com/apk/res/android">


<
data class = "MainActivityBinding"/>

<
android.support.constraint.ConstraintLayout
</android.support.constraint.ConstraintLayout>
</
layout>

Always remember to clean and rebuild your project.

Using Data binding technique

Originally using the traditional view binding technique, we inflate the xml layout into the activity using the setContentView(R.layout.activity_main) . Then we find a view in the layout using the findViewById() method. View findViewById(int resourceId) returns a View object. The ID passed is used to identify the view in the layout the activity inflates. The instance of that view is then stored in a local variable.

Using the data binding techinque, an instance of the generated binding class is created and used to set the layout. From the binding instance, we can access the id of the views present in the layout without using the findViewById method.

The layout expression language allows you to compute and convert values, utilize collections, and apply conditional logic (pass values to a format string, get a collection item based on key or index, set a value or state based on a boolean).

The next article in this series will explain using expression language in data binding, creating bound layouts, using binding utilities, custom binding and observables.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tobiloba Adejumo

Interested in biomarker development, software dev and ai, as well as psychology, history, philosophy, relationships. Website: tobilobaadejumo.com