To utilize View Binding with customized views, you need to follow the below process:
To customize a view, you need to create a custom view class by extending the base view class, such as RelativeLayout or LinearLayout.
If you have a layout file for your custom view, you need to inflate it in your custom view class constructor.
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.
In the build.gradle (app) file, add the following code to enable View Binding:
android {
buildFeatures {
viewBinding = true
}
}
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.
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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-12-10 11:00:00 +0000
Seen: 19 times
Last updated: Dec 09 '21