I’m trying to create a BindingAdapter in my custom...
# android
p
I’m trying to create a BindingAdapter in my customer view but the changes won’t take effect. As an example the custom view can take in a color and draw a rounded rectangle with it. Everything works correctly reading the custom attributes from attrs file, where the color is fetched in the CustomView’s init{} block, then the color of the paint object is set to this color and in onDraw() the shape is drawn correctly. However if I try to use a BindingAdapter with Data Binding Library, the color isn’t applied. The code for the BindingAdapter is inside the custom view like this:
Copy code
companion object {
        @BindingAdapter(“app:bgColor”)
        @JvmStatic
        fun setColor(view: ElevatedFrameLayoutWithBinding, color: String) {
            view.backgroundPaint.color = Color.parseColor(color)
        }
    }
Edit: After further investigation when using the above code onDraw() for the custom view isn’t called, however if I add the attribute normally without Data Binding and the BindingAdapter onDraw() is called as normal.
Turns out I had to use setWillNotDraw(false) inside the binding adapter to ensure onDraw() was called.