Home » Android » android – How to force to remove keyboard after shown with SHOW_FORCED

android – How to force to remove keyboard after shown with SHOW_FORCED

Posted by: admin June 15, 2020 Leave a comment

Questions:

I show keyboard with code

        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                .toggleSoftInput(InputMethodManager.SHOW_FORCED,
                        InputMethodManager.HIDE_IMPLICIT_ONLY);
        etContent.requestFocus();

In next step I inflate new LinearLayout and call setContentView(newLayout) and keyboard is still there. How to force to remove keyboard ? I tried with

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

but it didn’t help. Can somebody suggest me solution ?

How to&Answers:

Try this out. I’ve used this to hide the soft input a number of times.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getContentView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Answer:

Try this, it should work

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getContentView().getWindowToken(), 0);