code: public static void addKeyboardListener(@NonNull final View rootView,
              @NonNull final OnKeyboardShowListener listener) {
  
          final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
              private boolean isKeyboardShown;
              private int initialHeightsDiff = -1;
  
              @Override
              public void onGlobalLayout() {
                  final Rect frame = new Rect();
                  rootView.getWindowVisibleDisplayFrame(frame);
  
                  int heightDiff = rootView.getRootView().getHeight() - (frame.bottom - frame.top);
                  if (initialHeightsDiff == -1) {
                      initialHeightsDiff = heightDiff;
                  }
                  heightDiff -= initialHeightsDiff;
  
                  if (heightDiff > 100) { // If more than 100 pixels, its probably a keyboard...
                      if (!isKeyboardShown) {
                          isKeyboardShown = true;
                          listener.onKeyboardShow(true);
                      }
                  } else if (heightDiff < 50) {
                      if (isKeyboardShown) {
                          isKeyboardShown = false;
                          listener.onKeyboardShow(false);
                      }
                  }
              }
          };
  
          rootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
          rootView.setTag(TAG_LISTENER_ID, layoutListener);
      }
docs: Registers listener for soft keyboard state changes.<br/>
  The state is computed based on rootView height changes.
  
  @param rootView Should be deepest full screen view, i.e. root of the layout passed to
  Activity.setContentView(...) or view returned by Fragment.onCreateView(...)
  @param listener Keyboard state listener