Sunday, 25 May 2014

How to hide keyboard softinputmode in android? By view or without - example

Okay, here is another short code describing how to hide soft keyboard in android. I usually use this second code because I don't have to bother with specifying view. It is very important to call hide keyboard when all elements were initialized.

//1 method - you specify view with focus
public void hideKeyboard(View view) {  
      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);  
      if (view != null) {  
           inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);  
//  2 method - gets any view which has focus
public void hideKeyboard(Activity activity) {  
      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);  
            View cur_focus = activity.getCurrentFocus();  
      if (cur_focus != null) {  
           inputMethodManager.hideSoftInputFromWindow(cur_focus.getWindowToken(), 0);