When we perform any action on button click and many times we have seen that the keyboard is open and the further activities are going on. The opened keyboard looks very bad on the screen. So to over come this problem here is the method to close the keyboard if opened.
You just need to call hideKeyboard method and have to pass the activity context as parameter.
public static void hideKeyboard(Activity activity) {
//start with an 'always hidden' command for the activity's window
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//now tell the IMM to hide the keyboard FROM whatever has focus in the activity
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
View currentFocusedView = activity.getCurrentFocus();
if (currentFocusedView != null) {
inputMethodManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), 0);
}
}
0 Comment(s)