Software keyboard control

Add soft keyboard listener:

final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                if (heightDiff > 100) { // 99% of the time the height diff will be due to a keyboard.
                    if (!isSoftKeyboardOpen) {
                        MyLogger.log(TAG, "Soft keyboard is visible");
                        isSoftKeyboardOpen = true;
                    }
                    isSoftKeyboardOpen = true;
                } else if (isSoftKeyboardOpen) {
                    MyLogger.log(TAG, "Soft keyboard invisible");
                    isSoftKeyboardOpen = false;
                    removeFocusAndSaveToDB();
                }
            }
        });

Opens soft keyboard when alert dialog shown:

alertDialog.getWindow()
    .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.