Why we use ViewTreeObserver#addOnGlobalLayoutListener()

If you hadn't used ViewTreeObserver, than mainLayout.getRootView().getHeight() would simply return 0px, because it hasn't been laid out yet (see getWidth() and getHeight() of View returns 0).

Thus, you are waiting until view is measured, laid out, and then you are fetching width/height values from it. This callback will be fired exactly when the view is going to be laid out on the screen.


Don't know why but this was the first page shown to me when i searched for KOTLIN and after passsing Lamda i was unable to remove listner.

This is how i did in kotlin

tvLoginWith.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
        override fun onGlobalLayout() {
            tvLoginWith.viewTreeObserver.removeOnGlobalLayoutListener(this)
            tvLogin.layoutParams.width = tvLoginWith.width
            tvLogin.requestLayout()
        }
    })

Kool way to do this in kotlin (Reusable) create extension like this

fun ViewGroup.addViewObserver(function: () -> Unit) {
    val view = this
    view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            view.viewTreeObserver.removeOnGlobalLayoutListener(this)
            function.invoke()
        }
    })
}

And use it from activity like this

listThumb.addViewObserver {
 // your code
}

listThumb is recyclerview in this case