DEV Community

Cover image for View Binding vs Data Binding Gotchas
Vincent Tsen
Vincent Tsen

Posted on • Updated on • Originally published at vtsen.hashnode.dev

View Binding vs Data Binding Gotchas

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

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>
Enter fullscreen mode Exit fullscreen mode

Enable View Binding

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

buildFeatures {  
    viewBinding true  
}
Enter fullscreen mode Exit fullscreen mode

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   
}
Enter fullscreen mode Exit fullscreen mode

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  
}
Enter fullscreen mode Exit fullscreen mode

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

Unresolved reference: FragmentFirstBinding
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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  
}
Enter fullscreen mode Exit fullscreen mode

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


Originally published at https://vtsen.hashnode.dev.

Top comments (0)