Use view binding to replace findViewById
New in Android Studio 3.6, view binding gives you the ability to replace findViewById
with generated binding objects to simplify code, remove bugs, and avoid all the boilerplate of findViewById
.
TL;DR
- Enable view binding in
build.gradle
(no libraries dependencies) - View binding generates a binding object for every layout in your module (
activity_awesome.xml
→ActivityAwesomeBinding.java
) - Binding object contains one property for every view with an id in the layout — with the correct type and null-safety
- Full support for both the Java programming language and Kotli
Update build.gradle
to enable view binding
You don’t need to include any extra libraries to enable view binding. It’s built into the Android Gradle Plugin starting with the versions shipped in Android Studio 3.6. To enable view binding, configure viewBinding
in your module-level build.gradle
file.
See more at : https://medium.com/androiddevelopers/use-view-binding-to-replace-findviewbyid-c83942471fc