8

How do I close the keyboard on button click? I have a fragment which has an EditText and two buttons. One submits the EditText content, the other simply closes the fragment. Now when the fragment is gone, the keyboard stays. However, pressing the back button closes the keyboard or clicking on "done" also closes it. But what I need is the keyboard disappear when the fragment is closed.

I've tried solutions on similar questions here,here or here but none seems to work. Most of them throw a NullPointerException. All are for activities not fragments. The code for calling 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 will be appreciated.

Community
  • 1
  • 1
esQmo_
  • 1,204
  • 1
  • 15
  • 33

4 Answers4

19

Use this method

public void hideKeyboard() {
    // Check if no view has focus:
    View view = getActivity().getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
sreejith v s
  • 1,238
  • 9
  • 17
  • 1
    Works now: I was calling getActivity().onBackPressed(); that close the fragment, before your code. I've just inverted them. – esQmo_ Mar 28 '17 at 05:39
6

for a fragment use the following function

  public static void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

call it when the button is clicked

  btn_cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hideKeyboard(getActivity());
        }
    });
Vipin Singh
  • 104
  • 3
3

Try below method

public static void hideKeyboard(Context mContext) {

    try {

        View view = ((Activity) mContext).getWindow().getCurrentFocus();

        if (view != null && view.getWindowToken() != null) {

            IBinder binder = view.getWindowToken();

            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(binder, 0);

        }

    } catch (NullPointerException e) {

        e.printStackTrace();

    }

}

In this method you have to pass context parameter. Hope it will help you out.

Rahul Sharma
  • 8,050
  • 6
  • 18
  • 29
1

Evolving from previous answers and in Kotlin, using the calling view to obtain the window token.

button.setOnClickListener() { view ->
        hideKeyboard(view)
}

private fun hideKeyboard(view: View) {
    val inputMethodManager = view.context.getSystemService(Activity.INPUT_METHOD_SERVICE)
            as InputMethodManager

    inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}

After more thought, instead of having to add this call to every button, it may make more sense to clear the keyboard on loss of focus.

input.setOnFocusChangeListener { view, hasFocus ->
        if(!hasFocus) {
            hideKeyboard(view)
        }
    }
Montwell
  • 427
  • 3
  • 12