code: public static void executeOnGlobalLayout(@NonNull final View view, @NonNull final ViewTreeObserver.OnGlobalLayoutListener listener) {
          ViewTreeObserver vto = view.getViewTreeObserver();
          vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                      view.getViewTreeObserver()
                              .removeGlobalOnLayoutListener(this);
                  } else {
                      view.getViewTreeObserver()
                              .removeOnGlobalLayoutListener(this);
                  }
  
                  listener.onGlobalLayout();
              }
          });
      }
docs: Helper that removes the drudgery of using ViewTreeObserver
  
  @param view
  @param listener