Android : Difference between DataBinding and ViewBinding

34,454

Solution 1

According to the official docs:

ViewBinding

Only binding views to code.

DataBinding

Binding data (from code) to views + ViewBinding (Binding views to code)

There are three important 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 BindingAdapters 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.

In short, there is nothing viewbinding can do that databinding cannot do (though at cost of longer build times) and there are a lot databinding can do that viewbinding can"t

Solution 2

ViewBinding VS Databinding

Here is why I wanted to explain ViewBinding and DataBinding together. enter image description here

As you can see, ViewBinding is a sort of subset of DataBinding libraries which means ViewBinding and DataBiding can do the same jobs in terms of binding layouts. And it would also mean with DataBinding, you might not need ViewBinding cause it will do what ViewBinding is supposed to do and also provide a bit of an extra functionalities such as 2way binding, and using variables in XML files.

Then, this can lead to a question

"Then let's just use DataBinding as it sounds much more fancy!"

Hold on. As much as it sounds fancy, it is a pretty heavy loaded library which might cause a longer compile time. So if you are not going to use DataBinding only functionalities then might be better to consider ViewBinding as it does have some advantages in terms of build time and apk size.

For more detail read this article

Solution 3

By the official definitions,

View binding allows us 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.

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.

Differences View Binding and Data Binding

  1. View Binding library is faster than Data Binding library as it is not utilising annotation processors underneath, and when it comes to compile time speed View Binding is more efficient.

  2. The one and only function of View Binding is to bind the views in the code. While Data Binding offers some more options like Binding Expressions, which allows us to write expressions the connect variables to the views in the layout.

  3. Data Binding library works with Observable Data objects, you don't have to worry about refreshing the UI when underlying data changes.

  4. Data Binding library provides us with Binding Adapters.

  5. Data Binding library provides us with Two way Data Binding, this is a technique of binding your objects to xml layouts, so that both object and layout can send data to each other.

To understand View Binding and Data Binding in detail you can go through these articles,

https://medium.com/geekculture/android-viewbinding-over-findviewbyid-389401b41706

https://anubhav-arora.medium.com/android-view-binding-v-s-data-binding-5862a27524e9

Share:
34,454
Pratik Butani
Author by

Pratik Butani

Pratik Butani is Android/Flutter Lead at 7Span - Ahmedabad. He is working with Flutter Development since 2020 and Android Development since 2013. He is on the list of Top 100 User’s (85th) in India and Top 10 User’s (6th) in Gujarat as Highest Reputation Holder on StackOverflow. He was co-organizer at Google Developer Group – Rajkot in 2014-17. All-time Learner of new things, Googler and Eager to Help IT Peoples. He is also likes to share his knowledge of Android and Flutter with New Learner. SOReadyToHelp ツ Fell in Love with Android ツ I really feel proud to up-vote My Favorite #SO friend. => Android Application Developer by Passion :) => Being Helpful by Nature ;) => Now in List of Top 100 User's (85) in India and Top 10 User's (6) in Gujarat as Highest Reputation Holder on StackOverflow => Visit my blogs for learning new things : Happy to Help :) :) => My Apps on Playstore: AndroidButs & PratikButani => My Articles on Medium => Join Me on LinkedIn => Tweet Me on Twitter => Follow Me on Quora - Top Writer on Quora in 2017 => Hangout with Me on [email protected] => Social Networking Facebook => Get Users list whose bio's contains given keywords More about me :) -> Pratik Butani

Updated on July 08, 2022

Comments

  • Pratik Butani
    Pratik Butani almost 2 years

    We are using DataBinding since Jetpack release. Android documentation indicates that ViewBinding was added in Android Studio 3.6 Canary 11+.

    I read the documentation but its looks similar to DataBinding.

    Can anyone explain what's the difference between these two concepts?