View Binding vs Data Binding Gotchas
Tags: android, beginners
Published: 2021-11-13 00:34:53

**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
<?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()`
```kotlin
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 a`layout` root tag in your fragment layout.  

## You Need `layout` Root Tag
```xml
<?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
- [DataBindingUtil.inflate vs View Binding Inflate](https://vtsen.hashnode.dev/databindingutilinflate-vs-view-binding-inflate)

---

*Originally published at [https://vtsen.hashnode.dev](https://vtsen.hashnode.dev/view-binding-vs-data-binding-gotchas)*.