Skip to content

Instantly share code, notes, and snippets.

@snadjafi
Last active March 10, 2017 07:40
Show Gist options
  • Save snadjafi/520d7ac377ecdfd69c98 to your computer and use it in GitHub Desktop.
Save snadjafi/520d7ac377ecdfd69c98 to your computer and use it in GitHub Desktop.
TreeObserver example
mView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
int width = mView.getWidth();
int height = mView.getHeight();
if (mView.getViewTreeObserver().isAlive()) {
// remove this layout listener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mView.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
} else {
mView.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment