Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : Difference between DataBinding and ViewBinding

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?

like image 443
Pratik Butani Avatar asked Sep 21 '19 13:09

Pratik Butani


People also ask

What is the difference between ViewBinding and data binding?

View binding is the process of connecting views to each other. View binding is the simpler of the two methods. All you need to do is specify the name of the view you want to bind to, and Android will take care of the rest. On the other hand, data binding requires you to write code to connect data to views.

Can we use DataBinding and ViewBinding together?

Yep, we don't use <layout> ViewBinding, because on DataBinding we must add that layout tag to generate Binding class and it's different with ViewBinding which automatically generates all layout to Binding class.

When should I use DataBinding?

With data binding, a change to an element in a data set automatically updates in the bound data set. Data binding may be used for many reasons, such as to link an application's user interface (UI) and the data it displays, for data entry, reporting and in text box elements.

What is the difference between one way data binding and two way data binding Android?

In one-way binding, the flow is one-directional. In a two-way binding, the flow is two-directional. This means that the flow of code is from ts file to Html file. This means that the flow of code is from ts file to Html file as well as from Html file to ts file.


1 Answers

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

like image 57
Oluwasegun Wahaab Avatar answered Oct 04 '22 00:10

Oluwasegun Wahaab