View Binding got released as part of Android Jetpack
Docs: https://developer.android.com/topic/libraries/view-binding
My question is, how to use view binding with custom views. Google documentation has only show-cased Activity and fragment.
I tried this, but nothing was shown.
LayoutInflater inflater = LayoutInflater.from(getContext());
And then, I used this one, but again, no luck.
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
I guess maybe I don't target the correct layout inflater for my view but not sure.
ViewBinding is always null safe and type-safe, which supports both Java and Kotlin. ViewBinding is introduced in the Gradle version 3.6 and above (which comes with the Android Studio 4.0, only gradle 3.6). ViewBinding also helps to reduce the boilerplate code, hence reducing the code redundancy.
Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.
Inflates a binding layout and returns the newly-created binding for that layout. static <T extends ViewDataBinding> T. inflate(LayoutInflater inflater, int layoutId, ViewGroup parent, boolean attachToParent) Inflates a binding layout and returns the newly-created binding for that layout.
Just inform the root, and whether you want to attach to it
init { // inflate binding and add as view binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this) }
or
init { // inflate binding and add as view binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this, true) }
which inflate method to use will depend on the root layout type in xml.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With