Noticing when a view is completely drawn, with a Kotlin extension

suggest change
mView.afterMeasured {
  // inside this block the view is completely drawn
  // you can get view's height/width, it.height / it.width
}

Under the hood

inline fun View.afterMeasured(crossinline f: View.() -> Unit) {
    viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            if (this@afterMeasured.measuredHeight > 0 && this@afterMeasured.measuredWidth > 0) {
                this@afterMeasured.viewTreeObserver.removeOnGlobalLayoutListener(this)
                this@afterMeasured.f()
            }
        }
    })
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents