Created
November 1, 2013 14:41
-
-
Save dominicthomas/7266405 to your computer and use it in GitHub Desktop.
How to add and remove a viewtreeobserver to an android view. This lets you call stuff when the view is finished rendering.
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
final ViewTreeObserver vto = myview.getViewTreeObserver(); | |
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { | |
@SuppressLint("NewApi") | |
@Override | |
public void onGlobalLayout() { | |
// do stuff | |
// remove this layout listener - as it will run every time the view updates | |
if (myview.getViewTreeObserver().isAlive()) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
myview.getViewTreeObserver() | |
.removeOnGlobalLayoutListener(this); | |
} else { | |
myview.getViewTreeObserver() | |
.removeGlobalOnLayoutListener(this); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment