Hiding the keyboard in a fragment

I get these errors when trying to hide the keyboard inside a fragment inside an action:

Error: Cannot resolve getSystemService

Unable to resolve context

Cannot resolve getCurrentFocus ()

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
+6
source share
1 answer

Inside the fragment you should use getActivity (),

  InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
+10
source

Source: https://habr.com/ru/post/989295/


All Articles