Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To utilize View Binding with customized views, you need to follow the below process:

  1. To customize a view, you need to create a custom view class by extending the base view class, such as RelativeLayout or LinearLayout.

  2. If you have a layout file for your custom view, you need to inflate it in your custom view class constructor.

  3. In your custom view class, initialize your views and layout elements as usual, but instead of using findViewById, you use View Binding to bind the views.

  4. In the build.gradle (app) file, add the following code to enable View Binding:

    android {
       buildFeatures {
           viewBinding = true
       }
    }
    
  5. In the layout XML file, add the following code to enable View Binding:

    <LinearLayout
       ...
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       tools:viewBindingIgnore="true">
    

    The viewBindingIgnore attribute is added to ignore View Binding for this layout file, as View Binding is already used in the custom view class.

  6. Finally, you can use your customized view in any activity or fragment by simply adding it to the layout XML file and referencing it by its name.