I need to know correct widths, heights and positions of some of the views in my Activity
ASAP after onResume
. Where can I place this code in my Activity?
Asked
Active
Viewed 3,177 times
0

Andrey Novikov
- 5,563
- 5
- 30
- 51
1 Answers
4
Oddly, I get to reuse an answer the same day I make it:
You could set a global layout listener on the view tree in your onResume()
and do your diddling in there.
final ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
// do layout tweaking based on measurements
// remove the listener... or we'll be doing this a lot.
vto.removeGlobalOnLayoutListener(this);
}
}

Brian Dupuis
- 8,136
- 3
- 25
- 29
-
4I had to use `vto.isAlive()` and call `getViewTreeObserver()` again, otherwise it crashed with `java.lang.IllegalStateException: This ViewTreeObserver is not alive`. Anyway, thanks. – Andrey Novikov Feb 05 '12 at 20:56
-
Ah! Nice find and glad it helped. Best of luck. – Brian Dupuis Feb 05 '12 at 20:57
-
@AndreyNovikov can you show how exactly you did it ? – Danpe Mar 08 '16 at 18:27
-
@Danpe, see my answer here: http://stackoverflow.com/a/39001731/2914140 – CoolMind Aug 17 '16 at 16:26