Why we use ViewTreeObserver#addOnGlobalLayoutListener()

12,876

Solution 1

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.

Solution 2

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

Share:
12,876
Prasanth Yejje
Author by

Prasanth Yejje

Updated on June 28, 2022

Comments

  • Prasanth Yejje
    Prasanth Yejje almost 2 years

    Why do we use ViewTreeObserver, please can anyone explain it?

    In below code creditsView is TextView object. By this whole code I understand that "this is to hide some text based on condition", but only thing is why we are using ViewTreeObserver?

    mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = mainLayout.getRootView().getHeight() - mainLayout.getHeight();
    
                if (heightDiff > 100) {
                    Utils.appLogger("MyActivity", "keyboard opened");
                    creditsView.setVisibility(View.GONE);
                }
    
                if (heightDiff < 100) {
                    Utils.appLogger("MyActivity", "keyboard closed");
                    creditsView.setVisibility(View.VISIBLE);
                }
            }
        });
    
  • azizbekian
    azizbekian about 7 years
    @PrasanthYejje, as a newcomer, as somebody answers to your question, and you are satisfied with the answer, mark the answer as accepted.
  • CoolMind
    CoolMind about 5 years
    Agree, with lambda we strangely cannot use and remove listener.
  • Dr4ke the b4dass
    Dr4ke the b4dass about 3 years
    What if I'm using it only in xml, data-binding?
  • Aklesh Singh
    Aklesh Singh almost 3 years
    @CoolMind i guess its like how in javascript lamdas functions don't have their own this (context). lexical scopes
  • CoolMind
    CoolMind almost 3 years
    @AkleshSingh, yes, it sounds properly.