I am defining some animations based on the inflated dimensions of some UI controls. What is the earliest point in the Activity life cycle I can tap into to know when the UI elements have been sized and I can query them for their dimensions?
-
1Here lies the answer you seek. http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r – DeeV Mar 09 '12 at 19:51
-
@Deev put that as an answer and I'll mark it accepted. That was the solution I was looking for and it worked – Rich Mar 10 '12 at 01:34
-
Ok, done. I extended it a bit just so it's an "answer". – DeeV Mar 12 '12 at 14:25
3 Answers
Right after you set the Content of you Activity via the setContentView()
method is the earliest I've been able to grab information from my widgets (size, text and others).

- 4,752
- 3
- 27
- 41
Per Rich's request:
You can determine when the width and height by using the GlobalLayoutListener
like so:
final View myView = findViewById(R.id.id_of_view);
ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int viewHeight = myView.getHeight();
int viewWidth = myView.getWidth();
// Do what you want with the width and height
ViewTreeObserver obs = myView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
Full (better) answer: How to retrieve the dimensions of a view?
If you want to drill down to the point that widget have JUST been placed you have to extend each widget you want to monitor. Then override onDraw method and capture if that view has been drawn one time
private boolean imVisible=false;
public boolean imVisible() {
return imVisible;
}
@Override
protected void onDraw(Canvas canvas) {
if(!imVisible){
imVisible=true;
}
super.onDraw(canvas);
}
Then you can do a for loop to the widgets of interest and you know they are drawn at their position with dimentions.
A far better solution is when the onDraw gets called the first time fire a listener that is drawn. You have to set an array of listeners that watch the progress of the widgets. That way the exact moment that the last widget is on the screen... you know it.

- 9,284
- 8
- 53
- 78