public void hideKeyboard(Context context, View view) {
//If no view currently has focus
if (view == null) {
return;
}
Context context = view.getContext();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
usage:
// call from inside an Activity... hideKeyboard(this, view); hideKeyboard(this, getWindow().getDecorView()); hideKeyboard(this, findViewById(android.R.id.content)); hideKeyboard(this, getCurrentFocus());