How to Use View Binding in RecyclerView Adapter

David Sunday
The Startup
Published in
2 min readSep 7, 2020
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.

Advantages of view binding

  • Null safety: Since view binding creates direct references to views, there’s no risk of a null pointer exception due to an invalid view ID. Additionally, when a view is only present in some configurations of a layout, the field containing its reference in the binding class is marked with @Nullable.
  • Type safety: The fields in each binding class have types matching the views they reference in the XML file. This means that there’s no risk of a class cast exception.

Adding view binding to your new or existing project

View binding is enabled on a module by module basis. To enable view binding in a module, set the view binding build option to true in the module-level build.gradle file, as shown in the example below:

Simple RecyclerView adapter with view Binding

for more usage of view binding, use the link below

https://developer.android.com/topic/libraries/view-binding

--

--