ViewBinding as a replacement

Sayed El-Abady
3 min readJan 1, 2020

--

Yesterday was the last day of 2019 and it was a year full of new things in the Software Development World generally and in the Android Development World specifically and one of these things is ViewBinding, it was first announced at Google I/O event as a new way of accessing your XML layouts, so let’s start with knowing what is it

What is ViewBinding?

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.

As the documentation says, view binding allows you to access views from XML and handle what you want to do with them, that’s basically what findViewById does, what is the difference then? well, it comes with 2 important advantages;

Type safety:

You won’t have any risk of ClassCastException as the fields in the generated binding class will have types matching the views they reference in the XML file.

Null safety:

You won’t have any risk of NullPointerException due to an invalid ID either as view binding creates direct references to views, in case of multiple configurations with multiple layouts, a tablet for example, and your view is presented in only one layout of them it will be annotated with @Nullable as it might be null in this case.

All of this and the idea itself behind ViewBinding as it declares fields for your view in the generated classes make it a clean, safe and compile-time replacement for findViewById, now let’s get through an example.

Using ViewBinding

First, we need to know that View binding is available in Android Studio 3.6 Canary 11, this example works the same for a fragment, let’s get Started

Let the Gradle knows that we will be working with ViewBinding to, by doing that ViewBinding will generate a binding class for every XML layout, this generated class will follow PascalCase suffixed with Binding work, for example, if our XML file is activity_main it will be ActivityMainBinding.

android {
...
viewBinding {
enabled = true
}
}

Then make sure that your view has an id to be able to access it

ViewBinding will generate the binding class for you, like this:

Now we have to tell our Activity that we will be using the root of that generated binding instead of the ordinary XML reference, now you can access any view as a field from that binding variable and they will be named following camelCase convention.

NOTE: you can disable the generation of the binding class for any particular XML file by adding this attribute, tools:viewBindingIgnore="true", to your layout parent element.

Can ViewBinding replace DataBinding?

Sharing half of the name doesn’t mean necessarily that they can replace each other, ViewBinding is intended to be a clean replacement for findViewById not more than that and it’s not a replacement for DataBinding and if you feel like it can fit in your case then you are probably using DataBinding wrong and I strongly advise you not to do that as it has a lot of pitfalls and many things to take care of, will be talking about them in another article soon.

TLDR;

ViewBinding is a new clean replacement for findViewById with null and type-safety and it’s not a replacement for DataBinding.

Thanks for reading! Be sure to clap below and recommend this article if you liked it.

Reach me out on Twitter, Linkedin.

--

--