Question

Is there a way to get key events when forcing keyboard this way in Fragment onCreateView method:

    getActivity().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Also would need the keyboard to be in numeric mode.

Thanks.

Was it helpful?

Solution

You can listen keyboard presses if you override onKeyDown method in your activity

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            ...
            return true;
            ...
        default:
            return super.onKeyDown(keyCode, event);
        }
    }

OTHER TIPS

There are two methods to show soft keyboard even if you dont have any edit text to focus on,

first-

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInputFromWindow(viewToAnchorTo.getWindowToken(), 0);

Second-

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top