Last active
August 29, 2015 13:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it matter which
View
you get theViewTreeObserver
from?Also why did they deprecate the
removeGlobalOnLayoutListener()
method? Just to change the name to something that matches theadd...
method?