How to close the keyboard by pressing a button? I have a snippet that has an EditText and two buttons. One represents the contents of the EditText, the other just closes the fragment. Now that the fragment has disappeared, the keyboard remains. However, pressing the back button closes the keyboard or pressing done also closes it. But I need the keyboard to disappear when the fragment is closed.
I tried solutions on similar issues here , here or here , but no one works. Most of them throw a NullPointerException . All of them are for action, not fragments. The code to call the keyboard works:
editText.requestFocus(); InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
However, I had to add getActivity () to make it work.
Any help would be appreciated.
source share