Category: Keyboard

  • Hide/Show soft keyboard methods

    
    private void showSoftKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
    }
    

    (more…)

  • 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);