/** Runs a piece of code just before the next draw. */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static void doAfterDraw(final View view, final Runnable runnable) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return; final OnDrawListener listener = new OnDrawListener() { @Override public void onDraw() { view.getViewTreeObserver().removeOnDrawListener(this); runnable.run(); } }; view.getViewTreeObserver().addOnDrawListener(listener); }