7

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 ?

Damir
  • 54,277
  • 94
  • 246
  • 365

2 Answers2

7

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);
Noah
  • 15,080
  • 13
  • 104
  • 148
JoeLallouz
  • 1,338
  • 1
  • 10
  • 14
  • +1 from me, thank you. If you happen to see this comment: do you know why the `setSoftInputMode()` alone sometimes does the trick and sometimes not? –  May 14 '15 at 09:52
  • 1
    I'm not 100% sure, I imagine there are defaults for the method if you don't pass any arguments. – JoeLallouz Jun 11 '15 at 16:32
  • 1
    This is very helpful answer but `getContentView()` is not resolve in my Activity so, I just replace this by `getWindow().getDecorView()` . Now it is working perfectly for me. :) – Ekta Bhawsar Mar 06 '18 at 10:33
1

Try this, it should work

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getContentView().getWindowToken(), 0);
koder
  • 454
  • 6
  • 18