I'm reading about how the MVVM architecture works and how to use Android Data Binding Library help.
In a very general way I understand that Android Data Binding creates a link between UI layer and the underlying data model that holds the information to display.
Kotlin Android Extensions are another Kotlin plugin that will allow you to recover views from Activities, Fragments and Views. The plugin will generate some extra code that will allow you to access views in the XML layout, just as if they were properties with the name of the id you used in the layout definition.
What is the difference between using Android Data Binding Library and Kotlin Android Extensions? Are they for different purposes? Do they complement each other, in what way?
Thank you for your answers.
Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.
The Kotlin Android Extensions is a compiler plugin that allows you to access views from your XML directly in activities, fragments and views using what they refer to as synthetic properties. To use it, ensure that the plugin is enabled in your module's build.gradle file: apply plugin: 'kotlin-android-extensions'
Using data binding can lead to faster development times, faster execution times and more readable and maintained code. Android data binding generates binding classes at compile time for layouts.
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.
Both, Kotlin Android Extensions and Android Data Binding Library help to eliminate the use of findViewById
.
But there are also more things that these do, that can complement one another. To elaborate, with the Android Data Binding library, you can 'set' models in your xml files, which can then directly be leveraged to set values for the views in the layout. See how a <data>
tag can be used with the data binding library.
Kotlin android extensions does not provide for this. At the same time, Kotlin android extensions provides for some amazing features like @parcelize
annotation to make classes parcelable with almost no boilerplate code, etc.
To conclude, while they both eliminate the use of findViewById
, they have their own features too which can complement one another well.
Kotlin Android Extensions does not stand for only view binding. It contains other features as well. But I guess you’re talking about the view binding/caching features of Kotlin Android Extensions and wonder whether we still need data binding, since we already got rid of the findViewById calls with the synthetic properties of Kotlin. That was the question I asked to myself and my conclusion is, yes, data binding is still worth using.
From official documentation:
The Data Binding Library creates an immutable field in the binding class for each view that has an ID in the layout… The library extracts the views including the IDs from the view hierarchy in a single pass. This mechanism can be faster than calling the findViewById() method for every view in the layout.
So data binding doesn’t call findViewById on views one by one. Kotlin’s synthetic classes, on the other hand, still calls findViewById on the views under the hood, but it calls it only once for each view and caches the view reference for next calls. (Here is an article about it)
Besides, data binding has more to offer than only view caching. You can use data tags for passing data to the binding implementation and declare them in your xml, instead of setting them programmatically. This way you can get rid of the boilerplate code that you use for populating data, like those "setText"s, "setImageResource"s etc. You can set event listeners from xml using data binding. You can also come up with your own attributes using custom binding adapters. When used to its whole power, it can significantly reduce your Java/Kotlin code.
Edit: It seems that Google Android team recommends against use of kotlin synthetic properties. This article summarizes the discussion around this issue. And you can see in the new Udacity course prepared by Google that they use data binding as recommended practice.
Edit2: If you don't like the idea of "putting business logic in your xml", if you are not interested in setting or getting data from xml, if you simply want to avoid use of findViewByIds in a safe and efficient manner, than you can go with ViewDataBinding library instead. It is a simplified version of data binding library. It doesn't let you set data from your xml but it binds your views in a safe and efficient manner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With