Android Data Binding vs. View Binding

Şeyma Nur Fırat
3 min readDec 26, 2021
View binding vs data binding

Hello 🥳 Do you know how to use data binding or view binding? Let’s learn together!

What is the Data Binding?

The 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.

What does that mean? When you create binding object in one time, you can access views and it’s data from this object easily. You can get variables without using findViewById. But how? Let’s create an example 🚀

Firstly, we need to configure our project to use data binding. Enable the dataBinding option in app module’s build.gradle file.

To be able to use data binding you need to add <layout></layout> tags for all XML layouts.

We can pass the data in xml file using <data></data> tags.

How we can use data binding in activities?

In fragments? Let’s see!👯‍♀️

What is the View Binding?

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. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

While we use view binding, we don’t need to use findViewById as well.

Let’s create an example together 💁🏽‍♀️

Firstly, we need to configure our project to use view binding. Enable the viewBinding option in app module’s build.gradle file.

When we sync the project, Android SDK creates binding class to access layout files. These files have unique names. For example, if layout’s name is activity_main.xml, we can access to views with using ActivityMainBinding.

How to use view binding in activities?

Firstly, we need to create private lateinit var binding : ActivityMainBinding for access views.

ActivityMainBinding.inflate(layoutInflater) gets a static instance of the binding class created by inflate.

How to use in fragments?

Actually the usage for activities and fragments are similar. After we are done with the binding variable, we should set null it onDestroyView to avoid memory leaks.

ViewBinding vs. DataBinding

When we think about data binding and view binding, they could seem same but there are important differences between them.

· View binding has faster compile time than data binding. Because annotation process is not performed with view binding. So view binding is faster.

· Using view binding is easier than using data binding. Because you don’t need to take any action in XML file.

· If you want to use data binding, you should create <layout></layout> tags for the layouts where you want to use data binding. On the other hand with view binding, classes are automatically generated. In summary, layouts don’t need a <layout></layout> tags in view binding.

· In short, advantage of ViewBinding is it’s speed 🚀

I hope it was useful, happy coding 👩🏽‍💻🥳

🌻Sources

☕️ view binding, 🍪 data binding, 🍫 check it out.

--

--