I am getting these errors when trying to hide the keyboard inside a fragment within an activity:
Error: Cannot resolve getSystemService
Cannot resolve Context
Cannot resolve getCurrentFocus()
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
This can be called by passing Context and the view from currently displayed screen where you want to hide the keyboard: hideKeyboardFrom(getContext(), myEditView); There could be cases when there is no focusable view in Fragment and you want to hide the keyboard due to some event or trigger.
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.
Inside a fragment you should use getActivity(),
InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With