It is a feature that makes the interaction of code with views easy.
It generates a binding class for each XML layout.
The binding class contains a reference to all views that have an id in the layout.
It is used to replace the findViewById method.
Setup instructions
View binding is enabled on a module-by-module basis.
to allow view binding set the viewBinding build option to true in a module-level file.
If you want a layout file to be ignored while generating binding classes. add tools:viewBindingIgnore=”true” to the root view of the layout and no binding class will be generated for that layout file.
Different funcs of binding class to bind an object
inflate(inflater)— Use this in an Activity onCreate where there is no parent view to pass to the binding object.
inflate(inflater, parent, attachToParent) — Use this in a Fragment or a RecyclerView Adapter (or ViewHolder) where you need to pass the parent ViewGroup to the binding object.
bind(rootView) — Use this when you’ve already inflated the view and you just want to use view binding to avoid findViewById. This is useful for fitting view binding into your existing infrastructure and when refactoring code to use ViewBinding.
Note — You can only use inflate(inflater) method with activity BCS it doesn't have a parent nor a separate view generated. You can use inflate(inflater, parent, attachToParent) and bind(rootView) with fragment BCS it has a parent, and the view is also created separately.
Use view binding in Activity
before
after
Here you can see we have used inflate() method to inflate the layout.
here we have set the view using binding.root If you go into the implementation of root it will redirect you to the layout.
Use View Binding in Fragments
Using inflate(inflater, parent, attachToParent) method
we do the layout inflation and binding inside the onCreateView() method.
before
after
you can see in the above picture we have created a binding instance called binding.
we have also created a fragmentBlankBinding instance of FragmentBlankBinding and assigned a binding object to it.
Since binding is a local object we have to use fragmentBlankBinding object outside the onCreateView() method.
we have declared fragmentBlankBinding as nullable so that we can destroy the reference of the binding class when needed.
Fragment outlives views so it is better to remove any instance of the binding class in onDestroyView() the method.
You can see in the above ex that every time we have to do a null check while using fragmentBlankBinding the object. We will improve this using a getter method.
Another Approach
As you can see in the above example we have created a nullable object _binding . We do this so that we can destroy the binding instance once the view is destroyed.
we have created an object binding that will fetch _binding non-null values. By using binding we don't need to check for null or make a null safe call BCS it will always have a non-null value.
Using bind(rootView) method
Layout inflation happens in the onCreateView method and we do the binding in the onViewCreated method
Advantages over findViewById
Null safety — Since view binding allows direct reference to the views, there is no risk of null pointer exception due to an invalid view id.
Type safety — the fields in the binding class have types similar to the views they reference. So there is no typecast exception.
//findviewbyid example
public TextView textView2 = (TextView) findViewById(R.id.textView2); public Button button2 = findViewById(R.id.button2);public TextView textView1; //declaration
onCreate(){
textView1 = findViewById(R.id.textView1);
}
you can see in the above ex we can have a type casting error for the button2 view if it is not a button and instead a text view.
we can have a null pointer exception for textView1 if we don't define it inside the onCreate() method.
View Binding Vs Data Binding
both view binding and data binding generate a binding class that you can use to reference views directly but view binding is used for simple use cases and data binding for complex use cases and has few more properties than view binding.
Advantages of view binding over data binding
Faster compilation — view binding doesn't require annotation processing, so compile time is less.
Ease of use — view binding doesn't need special tagging for each layout as with data binding. just enable view binding for a module and it will apply to all the layout files automatically.
Limitations of view binding over data binding
layout variables or expressions — view binding doesn't support layout variables or expressions. so it cant be used to declare dynamic UI content straight from the XML layout file.
two-way data binding — view binding doesn't support two-way data binding. using one-way data binding we can set a value to an attribute of a view and create a listener that notifies us about changes in that value. two-way data binding provides a shortcut in which we can set the value as well as observe it.
Note — because of these features of view binding and data binding. It is advisable to use both in the project. a layout that requires simple features can use view binding. a layout that requires complex features can use data binding.