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)
WHAT IS THE MAIN PURPOSE OF DATA BINDING LIBRARY?
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.
HOW DOES DATA BINDING PERFORM ALL THESE MAGIC OF VIEW MANIPULATION?
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.
WHEN IS DATA BINDING ENABLED IN YOUR APPLICATION?
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.+
WHEN IS A LAYOUT SAID TO BE PROPERLY CREATED FOR DATA BINDING?
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.
Okay. Now that I’ve created a proper layout that allows for data binding, where will I find the generated class data binding creates for the layout?
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.
<data class = "MainActivityBinding"/>
Always remember to clean and rebuild your project.
What happens in the activity?
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
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.
Layout Expression Language
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.