Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding Keypad within Fragment

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);
like image 878
Lalit Jadiya Avatar asked Jun 17 '15 16:06

Lalit Jadiya


People also ask

How to hide keyboard on android studio in a fragment?

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.

How do I hide the keyboard on my Android?

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.


1 Answers

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);
like image 83
Firass Obaid Avatar answered Oct 07 '22 20:10

Firass Obaid