View Binding vs Data Binding - Explained | Android Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh hello there and welcome back to my new video in this video i'm going to explain you the main difference between a view binding and the data binding library so some of you who are just getting started into this world of android development don't even know about find view by id and how hard it was before on one hand you are a lucky guy and on the other hand you will not appreciate enough how helpful view binding and data binding are i know that a lot of you are still confused about the difference between view binding and data binding and you might be wondering should you use them together in your project or should you just use the data binding for everything in your project now i'm going to answer all those questions and explain you the main differences between those two libraries so first let's start with a definition so a data binding library is a support library that allows you to bind ui components in your layouts to a data sources in your app using a declarative format rather than programmatically then on the other hand view binding is a feature that allows you to more easily write code that interacts with the views and once view binding is enabled in a module it generates a binding class for each xml layout file present in that module and an instance of a binding class contains direct references to all views that have an id in the corresponding layout now unlike view binding where binding classes are automatically generated for all layout files in your project with data binding if you want to get the view reference you would need to convert each layout file to a data binding layout in order to generate the binding classes so if you want to enable a view binding in your project you will need to add this code into your gradle build file so you need to add the build features and inside you need to declare view binding to true or if you're using a data binding you just replace this view binding with a data binding and that's all you need to do to enable those two libraries so when it comes to generated binding classes you already know that unlike data binding where you would need to explicitly convert a layout file to a data binding layout to generate some binding classes for a view binding the compiler automatically generates binding classes for all your layout files in the project and sometimes you don't need all of them in that case you can add one simple attribute to a layout file for which you don't want to generate a binding class and you can just add this viewbinding ignore attribute to a ruth layout element and set its value to true so that's how you can stop generating a binding class for that specific layout file and now the most important question so what are the main differences between those two libraries so a view binding api is a faster than a data binding as it is not utilizing the annotation processors underneath and when it comes to compile time speed view binding is a more efficient so the one and only function of a view binding is to bind the views in the code while data binding offers some more options like binding expressions which allows you to write expressions that connect variables to the views in the layout then working with observable data objects so for example you don't have to worry about refreshing the ui when the underlying data source changes then we have a binding adapters so our binding adapters are responsible for making the appropriate framework calls to set the values and with the data binding we also have a two-way data binding so the data binding library supports two-way data binding and the notation used for this type of binding supports the ability to receive data changes to a property and listen to user updates to that property at the same time on the other hand view binding does not support layout variables or layout expressions so it can't be used to declare dynamic ui content straight from the xml layout files and the view binding does not support two-way data binding either so now you might be wondering should you use both of them or it would be an overkill well in some cases it is recommended to use both the view binding and the data binding in the project so you can use the data binding in layouts that require advanced features and use a view binding in the layouts that do not so the answer is yes you can use both of them in your project so that will be all for this video i hope that i have cleared the air and removed any confusions you had on this topic so like this video and comment down below if you would want to see more tutorials about some data binding features which i mentioned in this video thanks for watching this video and see you next time
Info
Channel: Stevdza-San
Views: 15,403
Rating: 4.9000001 out of 5
Keywords: android, studio, development, dev, app, data, binding, view, view binding, data binding, vs, versus, difference, between, bind, views, jetpack, library, binding adapters, adapter, two-way, expressions, explained, explain, how to, guide, tutorial
Id: IJ0jScwGu-8
Channel Id: undefined
Length: 5min 8sec (308 seconds)
Published: Fri Jan 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.