Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewGroup finish inflate event

I have created a custom layout which extends ViewGroup. Everything is working fine and I am getting the layout as expected.

I want to dynamically change an element in the layout. However this is not working as I am calling it in onCreate and till that time the entire layout is not actually (drawn) inflated onto the screen and hence do not have actual size.

Is there any event which can be used to find out when the inflation of the layout is done? I tried onFinishInflate but that would not work as Viewgroup has multiple views and this would be called multiple times.

I am thinking of creating an interface in the Custom layout class but not sure when to fire it?

like image 469
PravinCG Avatar asked Sep 22 '11 15:09

PravinCG


1 Answers

If I understand your requirements correctly, an OnGlobalLayoutListener may give you what you need.

  View myView=findViewById(R.id.myView);
  myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //At this point the layout is complete and the 
                //dimensions of myView and any child views are known.
            }
        });
like image 62
Phillip Fitzsimmons Avatar answered Nov 08 '22 08:11

Phillip Fitzsimmons