👨🏼‍💻ViewBinding vs DataBinding | Differences and Detailed

Sezer Ă–zaltun
Huawei Developers
Published in
6 min readNov 17, 2022

--

ViewBinding vs DataBinding

Introduction

Hello everyone,

We are together with a new medium article. In this article, What are View Binding and Data Binding that we use a lot in our Android projects?, Why do we use them?, and when should we use it? We’ll talk about things like these.

As you know, some projects use View Binding while others use Data Binding. So why do we choose to use them? Why do some android developers use View Binding and others use Data Binding? If you are looking for answers to questions like these, you are in the right here:)

What is View Binding ?

View Binding makes it easier and faster to bind your code to the relevant layout xml file. We would use findViewById to do this. We needed to define as many objects as the number of objects in our xml layout. Thanks to View Binding, we only need to define it once. That’s enough. After binding once using View Binding, we can create a variable and then we can access all objects in the layout xml using this variable.

View Binding vs findViewById

There are two fundamental differences.

  • Faster Compilation: View Binding does not require annotation, so compile time is faster.
  • Ease to Use: View Binding does not require us to define specially labeled layout xml files. We do not need to define separately for each widget. We define it only 1 time and it is created automatically. Therefore, it is faster to use.

How to use (Activity) ?

1- First we need to add the View Binding library into the build gradle(app).

build.gradle(app)

2- After adding it, let’s write the following codes in the Activity.

MainActivity

3- That’s all. After that, you can access the widget you want using the binding variable.

MainActivity

How to use (Fragment) ?

1- Let’s add the library to the build gradle(app) as we did in the use of Activity.

2- After writing the following codes in the fragment, you can access all the objects in the layout xml with the binding object.

CoinDetailFragment

What is Data Binding ?

Data Binding is the process of binding data to layout xml file. It allows you to keep your data and view it in sync. Data Binding can be one-way or two-way.

One-way: Changes in data are not reflected in the view.

Two-way: Changes in data are reflected in the view

With Data Binding, our design and code page are completely separated. We can even be more independent from the Activity by writing some functions in our layout xml. Thanks to the Binding Adapter, we can create custom functions.

How to use (Activity) ?

1- First we need to add the View Binding library into the build gradle(app).

buildgradle(app)

2- Let’s edit our layout xml file as follows. We just need to add the <layout> tag. I’ll explain the <data> part shortly.

layout.xml

3- After writing the following codes in the Activity, we can access all the objects in the layout xml using the binding variable.

MainActivity

How to use (Fragment) ?

1- Let’s add the library to the build gradle(app) as we did in the use of Activity.

2- Let’s edit our Layout xml file as follows. This time we fill in the <data> section. You can do the same process for Activity. I preferred to do it using Fragment.

Here, I can send data to design objects through the model in <data>. Thanks to the Binding Adapter, I can easily display images in ImageView.

3- Let’s create the Model Class. We will fetch the data from this class and display the data on the layout using Data Binding.

Book Model Class

4- Let’s create Binding Adapter to show image in ImageView.

Binding Adapter

5- We connect our row_item.xml file to the HomeAdapter class using Data Binding and here we send our data to our layout xml file using the binding.model = item code.

Home Adapter

6- As the last step, let’s write the necessary Data Binding codes in the Fragment. In order to see the data on the screen, we need to connect the HomeAdapter and the Fragment. Since our topic is Data Binding, I did not add those codes.

Home Fragment
Data Binding vs View Binding

View Binding vs Data Binding

  • View Binding is a way to bind your code to specific layout xml files. This allows you to access and modify view properties directly from your code.
  • Data Binding is a way to bind data to layout xml files. This allows you to bind data directly to your views without writing extra code.
  • As you can see in the image below, Data Binding encompasses everything View Binding does. In terms of binding schemes, both are the same. Data Binding also allows us to use variables from the XML file. So which one should we use?
View Binding vs Data Binding

Which Should I Use?

  • This depends on your needs.
  • View Binding is a good choice for accessing only the objects in your layout file.
  • Data Binding is a better choice if you need to send data to your objects in the Layout.
  • If you are using MVVM in your project, it would be better to use Data Binding.
  • Since we can also transfer data to the view with Data Binding, it will be more compatible with MVVM and clean code structure.

Data Binding and View Binding can be used together in our project. We can use Data Binding in views to which we transfer data, and View Binding in views that we do not transfer.

When should I use which one?

If you are developing a UI in your Android project, you have two options for accessing the objects in the layout. These are View Binding and Data Binding. Both have pros and cons, so it’s important to know when to use each.

  • View Binding is suitable for simple UIs with a few views. Installation is quick and easy. However, it may be less secure than Data Binding because it cannot check for errors at compile time.
  • Data Binding is more powerful than View Binding and it can be used for more complex layouts. It is also more reliable because it checks for errors at compile time.

Conclusion

Both View Binding and Data Binding have their own advantages and disadvantages, so which one you should choose depends on your needs. If you want to have a more organized code and you have a project that you want to transfer data from to layot xml file , you can use Data Binding. If you want to access Layout files faster and easier, but do not want to transfer data, you can use View Binding.

We have come to the end of another article. I hope it was a useful article for you. See you in the next article. Take care of yourselves.

References

--

--