Skip to content

Instantly share code, notes, and snippets.

@doridori
Last active August 29, 2015 13:56
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save doridori/9208247 to your computer and use it in GitHub Desktop.
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

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?

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