View binding : Android

Gajendra Singh Rathore
4 min readOct 28, 2020
View Binding — Android

Wow! yes you read it right. This was my reaction when I first read about View Binding and it’s usage.

Below are some questions which will be resolved if you read the blog.

  1. What is View Binding?
  2. How it is different from findViewById ?
  3. Implementing View Binding in
    - Activities
    - Fragments
  4. Link to some resources and Sample Projects.

Let’s get Started

What is View Binding?

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.

In most cases, view binding replaces findViewById.

In some simple words, For each XML file that we have in our project, View Binding generates a binding class, the instance of the binding class have direct references to the views that are present in the XML file and have an ID.

Let’s understand this with an example:
In activity_main.xml, we have a TextView with id tvName.
that TextView can be referenced in our Java/Kotlin Activity/Fragment file by a binding object(we’ll see this later in the article) using binding.tvName.

Are you confused? that’s good. keep reading…

How it is different from findViewById?

View binding has important advantages over using findViewById:

  1. Null Safety:
    As we read, View binding creates direct references to the views in XML, so there’s no risk of any null pointer exception as there won’t be any invalid view ID. Apart from this, Let’s say if our view is present in only portrait configuration and not in Landscape, In such situations when a view is only present in some of the configuration files then the field containing it’s reference in the binding class is marked with@Nullable.
  2. Type Safety:
    The fields in each binding class have types matching the views they reference in the XML file. Let’s take a look at this with one example, If we have a ImageView in XML then the binding class will have types matching views which will identify the view (in our case : ImageView) and will show methods for an ImageView only whenever needed. No other methods will be suggested and addressed so that there’s no risk of a class cast exception.

These differences mean that incompatibilities between your layout and your code will result in your build failing at compile time rather than at runtime.

View Binding Setup:

View binding is enabled on a module by module basis. If our project have multiple modules we can use View Binding in one or all as per our requirement. To enable view binding in a module, set the viewBinding build option to true in the module-level build.gradle file, as shown in the following example:

In case, you want a particular layout file to be ignored while generating binding classes, add the tools:viewBindingIgnore="true" attribute to the root view of that layout file:

Usage Basics

If view binding is enabled for a module, a binding class is generated for each XML layout file that the module contains. Each binding class contains references to the root view and all views that have an ID. The name of the binding class is generated by converting the name of the XML file to Pascal case and adding the word “Binding” to the end.

For example, given a layout file called activity_profile.xml:

The generated binding class is called ActivityProfileBinding. This class has two fields: a TextView called tvName and a Button called nextBtn. The ImageView in the layout has no ID, so there is no reference to it in the binding class.

Every binding class also includes a getRoot() method, providing a direct reference for the root view of the corresponding layout file. In this example, the getRoot() method in the ActivityProfileBinding class returns the LinearLayout root view.

Now we’ll see how to use View Binding in Activity and Fragments.

View Binding in Activity

The very first step us to setup View Binding that we’ve done above. Now, to set up an instance of the binding class for use with an activity, we need to perform the following steps in the activity’s onCreate() method:

  1. Call the static inflate() method included in the generated binding class. This creates an instance of the binding class for the activity to use.
  2. Get a reference to the root view by either calling the getRoot() method or using Kotlin property syntax.
  3. Pass the root view to setContentView() to make it the active view on the screen.

Let’s see how to achieve the above steps using code(Kotlin):-

Now, we can use the instance of the binding class to reference any of the views:

binding.name.text = viewModel.name
binding.button.setOnClickListener { viewModel.userClicked() }

View Binding in Fragments

The very first step us to setup View Binding that we’ve done above. Now, to set up an instance of the binding class for use with a fragment, perform the following steps in the fragment’s onCreateView() method:

  1. Call the static inflate() method included in the generated binding class. This creates an instance of the binding class for the fragment to use.
  2. Get a reference to the root view by either calling the getRoot() method or using Kotlin property syntax.
  3. Return the root view from the onCreateView() method to make it the active view on the screen.

Now, we can use the instance of the binding class to reference any of the views:

binding.name.text = viewModel.name
binding.button.setOnClickListener { viewModel.userClicked() }

Resources & Sample project

  1. Sample Project : ViewBinding
  2. Documentation : Android Docs
  3. Youtube: Android Developers

I hope you learn View Binding from the blog and enjoyed reading it, Make sure to Clap and Follow to learn about awesome topics and grow.
Enjoy Coding :)…

--

--