DEV Community

Cover image for View Binding vs. Data Binding
Zaccheaus Amenya
Zaccheaus Amenya

Posted on

View Binding vs. Data Binding

According to the official documentation, view binding just ties views, but data binding links views to both views and data from code.

Whats Binding?
By eliminating boilerplate code and minimizing the data representation portion of activities, binding makes it simple for us to bind (associate) layout files with activities.

View binding
We can develop code that interacts with views more quickly and simply thanks to view binding. When view binding is enabled in a module, each XML layout file contained in that module receives a binding class. All views having an ID in the associated layout are directly referenced by an instance of a binding class.

Benefits:
Speed Increase -> No need to use the findViewById procedure anymore, and removing a view won't cause any problems because errors will appear straight immediately.
Code Readability -> Giving the views reasonable ids will greatly improve the readability of the code.
*Reduces Crashes *-> If you never describe what a view is to be cast as you cannot make a mistake, type safety does this.

Data Binding
In simple terms, data binding is something that performs the same thing as view binding but adds a bit more to it and decreases the amount of work we have to do even more. First, as the name implies, we must tie the data to the views, which is precisely what occurs. The XML file specifies a data source, and when the view is presented, an instance of the data is also supplied, which may be used to automatically set attributes.

Benefits
Complexity Reduction-> Data Binding could be not so straight to start with, but once you get the hang of it will boost your development speed.
Reduces code -> The ease of letting go of updating a view multiple times can easily reduce many lines of code

Differences between View Binding and Data Binding

  • Layouts do not require a layout tag when using view binding.
  • Viewbinding cannot be used to connect layouts to data in xml (No binding expressions, no BindingAdapters, nor two-way binding with viewbinding)
  • The primary benefits of viewbinding are speed and efficiency. It offers a lower build time because it eliminates the overhead and performance difficulties related with databinding caused by annotation processors.
  • View Binding library is quicker than Data Binding library since it does not need annotation processors below, and View Binding is more efficient when it comes to compilation time speed.
  • View Binding's sole duty is to bind the views in the code. In comparison, Data Binding provides more choices, such as Binding Expressions, which allows us to build expressions that relate variables to layout views.
  • The Data Binding library uses Observable Data objects. When the underlying data changes, you don't have to worry about updating the UI.
  • Binding Adapters are provided by the Data Binding library.
  • The Data Connecting library includes Two-way Data Binding, which is a method of binding your objects to xml layouts so that both the object and the layout may transfer data to one other. In summary, there is nothing that viewbinding can accomplish that databinding cannot do (albeit at the expense of longer build times), and there are many things that databinding can do that viewbinding cannot.

Conclusion
While data binding may seem fascinating, it is a somewhat heavy-loaded library, which may result in a longer build time. So, if you're not just utilizing DataBinding for functionality, you might want to explore ViewBinding, which has some advantages in terms of build time and apk size.

Top comments (0)