0

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, because w and h 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.

Crocodile
  • 5,724
  • 11
  • 41
  • 67

0 Answers0