I've tested it on my API 19 real device and API 23 emulator and this method works fine:
if(activity != null){
View view = activity.getCurrentFocus();
if (view != null){
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputManager != null){
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
However, it's not working on my Pixel 4a (android 10) real device. Does anyone know how to close the keyboard programmatically on android 10?