Android : Difference between DataBinding and ViewBinding
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
With view binding, the layouts do not need a layout tag
You can't use viewbinding to bind layouts with data in xml (No binding expressions, no BindingAdapters nor two-way binding with viewbinding)
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
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
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.
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.
Data Binding library works with Observable Data objects, you don't have to worry about refreshing the UI when underlying data changes.
Data Binding library provides us with Binding Adapters.
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
ViewBinding VS Databinding
Here is why I wanted to explain ViewBinding
and DataBinding
together.
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