Android: View Binding vs Data Binding

View binding:

Joshua U
4 min readApr 20, 2022

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module.

How to use View Binding using Kotlin:

Step-1: In your module-level build.gradle file, add the following code to enable view binding.

android {      
// ...
buildFeatures {
viewBinding true
}
}

This automatically will create binding classes for each XML file present in that module.

If you don’t want to generate a binding class for a specific XML file, add the attribute tools:viewBindingIgnore="true" in the root layout like that:

<RelativeLayout
...
tools:viewBindingIgnore="true">
// ...
</RelativeLayout>

Using View Binding in Activities:

To use View Binding in Activity, create an instance of the binding class, get the root view, and pass it to setContentView().

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// ...
} }

Now, you can reference your views like that:

binding.myButton.setOnClickListener { // Button Tapped } binding.myTextView.text = "View Binding is the best!"

Data binding:

The Data Binding Library is an Android Jetpack library that allows you to bind UI components in your XML layouts to data sources in your app using a declarative format rather than programmatically, reducing boilerplate code.

Implementation of Data Binding:

Step-1: Enable Data binding in Android Application

To enable the usage of data binding in your Android application, add the following snippet to the app/build.gradle file.

android {
....
dataBinding {
enabled = true
}
}

Step-2: Open activity_main.xml file and add <layout> tags as the outermost tags of the layout file. We do that to tell data-binding library to create a binding object for this xml layout file.

<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools">...</androidx.constraintlayout.widget.ConstraintLayout></layout>

Step-3: First of all Rebuild the project. Rebuilding is necessary for Android Studio to generate databinding related classes for the layout.

Step-4: let’s go back to MainActivity and create an object of this newly generated ActivityMainBinding .

Open the MainActivity.kt .
Declare the object reference variable at the top of the class.

private lateinit var binding: ActivityMainBinding

Step-5: Then inside the onCreate funciton, construct the data binding instance replacing the default setContentView(R.layout.activity_main) function call with binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

Now you are ready to use data binding in the class like this:

Example:

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.anushka.bindingdemo1.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.submitButton.setOnClickListener {
displayGreeting()
}
}
private fun displayGreeting() {
binding.apply {
greetingTextView.text = "Hello! " + nameEditText.text
}
}
}

Why data binding ?

In Android Development we are using the findViewById() function to obtain references to views. But, every time we do so , Android system has to go through the view hierarchy and find it at runtime. Moreover , there can be many layouts and hundreds of views in a larger Android application.

Therefore, system will have to go through the view hierarchy again and again . That is a very inefficient and resource consuming process.

So, Google introduced Android data binding library as a solution for that problem. It generates a corresponding binding class for each xml layout.

After that, we can easily use an instance(object) of that binding class to set values to and get values from view components. Eliminating findViewById() is not the only benefit of data binding. Compare to previous approaches data binding has many advantages.

Advantages of Data binding:

  1. Eliminate findViewById()
  2. Update the values automatically.(doesn’t have to keep track of all the ways a value can be updated)
  3. Very effective for UI Testing.
  4. More readable code.
  5. More maintainable code.
  6. Faster development times.
  7. Faster execution times.
  8. Errors can be found during the compile time.
  9. Well suited for MVVM and MVI architectures.

View binding vs Data binding:

View binding: Only binding views to code

Data binding: Binding data(from code) to views + ViewBinding(binding views to code)

Few differences:

  1. With view binding, the layouts do not need a layout tag
  2. You can’t use viewbinding to bind layouts with data in xml (No binding expressions, no Binding Adapters nor two-way binding with viewbinding)
  3. The main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with databinding due to annotation processors affecting databinding’s build time.

References:

https://stackoverflow.com/questions/58040778/android-difference-between-databinding-and-viewbinding

https://johncodeos.com/how-to-use-view-binding-in-android-using-kotlin/

--

--

Joshua U

Python Enthusiast, Assistant Professor, Care for developing