Android Development Basics: How to add View Binding to an Android Gradle project

Most Android developers are probably familiar with the classic findViewById() method. Pass it an ID of one of the Views in your XML layout and it’ll return a reference to the inflated version of that View. That’s all assuming you passed the right ID though, and that the View actually exists. findViewById() has no checks built in to prevent you from trying to retrieve a View you can’t retrieve. Enter View Binding.
Instead of using findViewById() on each View you want, View Binding automatically generates a binding class for each layout XML. Each View with an ID is automatically added to the class, so you can reference them directly.
Adding View Binding to an Android Gradle project is super simple.
Gradle Setup
View Binding is enabled at the module level in Gradle. If you have multiple modules, you’ll need to enable it individually for each one.
In the android block in your module-level build.gradle, add the option to enable View Binding.
android {

    buildFeatures {

Written By: Zachary Wander

Original Article:

Leave a Comment