Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Grabbing content view dimensions via ViewTreeObserver - all Display methods have no guarantee if they will include system and status bars in height
//inside a fragment. If in an Activity you could use findViewById(Window.ID_ANDROID_CONTENT);
getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
//do something like measure a view etc
View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
Log.d("DISPLAY", content.getWidth() + " x " + content.getHeight());
//we only wanted the first call back so now remove
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
getView().getViewTreeObserver().removeGlobalOnLayoutListener(this);
else
getView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
@turbochan
Copy link

turbochan commented Mar 22, 2014

Does it matter which View you get the ViewTreeObserver from?

Also why did they deprecate the removeGlobalOnLayoutListener() method? Just to change the name to something that matches the add... method?

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment