Dismiss Keyboard on button click that close fragment

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);
    }
}

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());
        }
    });

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.