Marcos Vasconcelos Marcos Vasconcelos - 1 month ago 8
Android Question

Which Activity method is called after all the layout methods have been called?

I need to do something in an

Activity
after all the layout methods have been called, all the
View
s are in place and the
Activity
is ready to be displayed.

Which method can do that?

Answer

If you are trying to get a width of a view or something. This should work

Add this to your activity's onCreate

ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        // Put your code here. 

        layout.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
    } 
}); 
Comments