public static void addOnGlobalLayoutListener(final View view, final Runnable runnable) {
        ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                runnable.run();
            }
        });
    }
public static void addOnGlobalLayoutListener ( final View view , final Runnable runnable ) { ViewTreeObserver vto = view . getViewTreeObserver ( ) ; vto . addOnGlobalLayoutListener ( new ViewTreeObserver . OnGlobalLayoutListener ( ) { @ SuppressWarnings ( "deprecation" ) @ Override public void onGlobalLayout ( ) { if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . JELLY_BEAN ) { view . getViewTreeObserver ( ) . removeGlobalOnLayoutListener ( this ) ; } else { view . getViewTreeObserver ( ) . removeOnGlobalLayoutListener ( this ) ; } runnable . run ( ) ; } } ) ; }
Add an OnGlobalLayoutListener for the view.
<p>This is just a convenience method for using {@code ViewTreeObserver.OnGlobalLayoutListener()}.
This also handles removing listener when onGlobalLayout is called.</p>

@param view     The target view to add global layout listener.
@param runnable Runnable to be executed after the view is laid out.