50

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.

Jon
  • 693
  • 8
  • 17
  • Really good article about that: https://medium.com/google-developer-experts/exploring-view-binding-on-android-44e57ba11635 – SebastienRieu Feb 04 '20 at 15:04

3 Answers3

31

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.

Supriya
  • 1,940
  • 24
  • 23
  • 5
    Which one you prefer in case of generated code or build time? – Hemant Kaushik May 31 '18 at 07:30
  • Also, I just realized and like to add that KTX doesn't seem to provide concrete layout binding classes that can be used in code. It provides references only to the IDs specified within layout classes as field variables that can be used in code but not the entire layout itself. – Saifur Rahman Mohsin Sep 07 '19 at 11:18
31

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.

Oya Canli
  • 1,996
  • 1
  • 15
  • 27
1

I strongly disagree with the points mentioned above. maybe because I hate writing logic in XML. so both comments mention the use of <data> tags not found in Kotlin Android Extensions (KTX). with kotlin and KTX you can do better than data tag.

let's say we have

data class Person(val name:String, 
                   val phone:String,
                   val isMale:Boolean,
                   val isMarried:Boolean)

in the activity or fragment, we can do

fun updateView(data:Person){
    with(data){

     nameTextField.text = if(isMale){
                            "Mr. $name" 
                          } else {
                             if(isMarried){
                              "Mrs. $name"
                             }else{
                              "Miss $name"
                             }
                          }
     phoneTextField.text = phone
    }
 }

in data-binding, you have to do

android:text='@{person.isMale ? "Mr."+user.name: ((user.isMarried ? "Mrs. " : "Miss. ") + user.name)}'

KTX code is way cleaner than what you have to do with data binding to achieve the same result. when you need conditions to set values to view data binding gets ugly. so for me, Kotlin Android Extensions work better. I like my code clean. you can still use both the decision is yours to make.

Manzur Alahi
  • 1,870
  • 23
  • 19
  • 5
    You don't have to do it like that with databinding. In fact, you shouldn't do it like that. Use of complicated ternary operators in xml is not recommended, both for readibility, as you mentioned, and also because xml codes are not testable. So what you can do with databinding, is to use a helper method which will accept a person as argument and will return the text you need. – Oya Canli Apr 01 '19 at 21:54
  • since I can do this so easily with ktx and kotlin why bother using data binding + helper class!? – Manzur Alahi May 24 '19 at 20:27
  • 2
    I just wanted to clarify that you don't have to use "ugly" or unreadable ternary operators in data binding. You don't have to bother, of course, if it feels like "bothering". I guess it is about what someone is used to. For me, data binding means significantly less code in my activities and fragments. – Oya Canli Jun 12 '19 at 19:12
  • 1
    There are some arguments against Kotlin's synthetic properties though. Google team recommends data binding instead. This article summarizes the discussion about the issue: https://proandroiddev.com/the-argument-over-kotlin-synthetics-735305dd4ed0 But still, you don't have an obligation to follow Google's recommendations. It's up to you in the end. – Oya Canli Jun 12 '19 at 19:16
  • I read an article and found that kotlin android extension is not recommended in RecyclerView onBindView as calls findViewById under the hood. – Astha Garg Oct 05 '20 at 09:43
  • most of the things android offers are not really required unless you are building a really really complex app. Just use whatever suits you and try to not over complicate things. – Manzur Alahi Oct 05 '20 at 18:43