I want to call getCurrentFocus() out of activity or fragment to let the structure looks beautiful.But how can I call the method?Sometimes I use context as a parameter to achieve similar function.
You can do this by using Activity, Create a class named Utils and put the following code in it.
public class Utils{
public static void hideKeyboard(@NonNull Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
Now you can simply call this method in any Activity to hide keyboard
Utils.hideKeyboard(Activity MainActivity.this);
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