Browse Category

Keyboard

Hide/Show soft keyboard methods

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

Keep Reading

Software keyboard control

Add soft keyboard listener:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

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