Vincent Tsen
Android Kotlin Weekly

Android Kotlin Weekly

View Binding vs Data Binding Gotchas

Do I still need View Binding if I have already enabled Data Binding in my Android project?

Vincent Tsen's photo
Vincent Tsen
·Oct 22, 2021·

2 min read

View Binding vs Data Binding Gotchas

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • Enable View Binding
  • Enable Data Binding
  • You Need layout Root Tag
  • Enable Both View and Data Bindings
  • Summary
  • See Also

Let's say you have a fragment layout as below, and you want to add a View Binding into your project.

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  

    <TextView  android:id="@+id/textView"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="TextView" />  

</LinearLayout>

Enable View Binding

You enable this viewBinding in your build.gradle(module).

buildFeatures {  
    viewBinding true  
}

Then, you inflate the fragment layout in onCreateView()

override fun onCreateView(  
    inflater: LayoutInflater, container: ViewGroup?,  
  savedInstanceState: Bundle?  
): View? {  

  val binding = FragmentFirstBinding.inflate(inflater)  
    // access view in this fragment  
  val textView = binding.textview  
  return binding.root   
}

You can now access the textView from the binding without using findViewById(). Later on, you want to bind your data into the layout.

Enable Data Binding

You replace viewBinding with dataBinding as below:

buildFeatures {  
    dataBinding true  
}

Then, your code suddenly doesn't compile with this error:

Unresolved reference: FragmentFirstBinding

So, what happened? This is because when you use dataBinding, the FragmentFirstBinding is not auto generated, You need to define alayout root tag in your fragment layout.

You Need layout Root Tag

<?xml version="1.0" encoding="utf-8"?>  
<layout xmlns:android="http://schemas.android.com/apk/res/android">  

     <LinearLayout android:orientation="vertical"  
          android:layout_width="match_parent"  
          android:layout_height="match_parent">  

              <TextView  
                  android:id="@+id/textView"  
                  android:layout_width="match_parent"  
                  android:layout_height="wrap_content"  
                  android:text="TextView" />   

     </LinearLayout>

</layout>

You realize there are still other errors because other layouts haven't defined the layout root tag. In that case, you have 2 options here.

  • Add the layout root element to all your layouts
  • Enable both viewBinding and dataBinding

Enable Both View and Data Bindings

When you use dataBinding, not all layouts require to bind the data, and you're lazy to add the layout root tag (which is not required), you can turn on both options.

buildFeatures {  
    viewBinding true
    dataBinding true  
}

Summary

Do I still need viewBinding if I have already enabled dataBinding in my Android project?

The answer is NO if layout root tag has been defined in all your layout files. This is also called "Data Binding Layout". Otherwise, the answer is YES.

See Also

Did you find this article valuable?

Support Vincent Tsen by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this