Skip to content

Instantly share code, notes, and snippets.

@z8888q
Forked from doridori/ViewTreeObserver_Ex.java
Created April 5, 2014 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z8888q/9994682 to your computer and use it in GitHub Desktop.
Save z8888q/9994682 to your computer and use it in GitHub Desktop.
//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