Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Have All The Views In A Fragment Been Given A Size?

Without resorting to the infamous onGlobalLayoutListener() solution, and without having to implement a custom View, what lifecycle event in a Fragment can I put code into and be sure all of the Fragment's Views have been given a size?

As a corollary, I would also like this lifecycle event to be applicable to Fragments in a ViewPager.

like image 248
Christopher Perry Avatar asked Oct 30 '22 04:10

Christopher Perry


1 Answers

I dont think there would be any Fragment lifecycle event to be sure all the Views have size.

What I would usually do is, to use OnLayoutChangeListener inside onActivityCreated(). Like this,

getView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                getView().removeOnLayoutChangeListener(this);

                // Check the size of Views here.
            }
        });
like image 70
Bob Avatar answered Nov 15 '22 06:11

Bob