My custom view extends the AppCompatImageView
class and it needs to know its parent view's dimensions. The following code is used:
val w = (parent as ViewGroup).width
val h = (parent as ViewGroup).height
The problem:
I am not sure where to implement this code. Ideally, I'd like it to be called only once per layout change and follow standard practice.
I tried so far:
overwriting the following methods of the View class
onAttachedToWindow
⬅ This won't work, becausew
andh
are always 0.onMeasure
⬅ It's not ideal. It yields zero for the first time and it's called multiple times.onLayout
⬅ It's not ideal as it's called multiple times as well.