Blog Archive

Android hide and show soft keyboard

Let's say you are done entering text in a SearchView or EditText and based on your selection you want to take some action and as well as close(hide) the soft keyboard. You can force Android to hide the soft keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing the reference to the EditText,SearchView, etc.

public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)

Synonym for hideSoftInputFromWindow(IBinder, int, ResultReceiver) without a result: request to hide the soft input window from the context of the window that is currently accepting input.
Parameters
  • windowToken The token of the window that is making the request, as returned by View.getWindowToken().
  • flags Provides additional operating flags. Currently may be 0 or have the HIDE_IMPLICIT_ONLY bit set.
SearchView searchView = (SearchView) findViewById(R.id.search);

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(searchView.getWindowToken(), 0);

The soft keyboard should show up when you touch the EditText or SearchView, but in case it's not coming up you can force it to show up using InputMethodManager, calling showSoftInput.

public boolean showSoftInput (View view, int flags)

Synonym for showSoftInput(View, int, ResultReceiver) without a result receiver: explicitly request that the current input method's soft input area be shown to the user, if needed.
Parameters
  • view The currently focused view, which would like to receive soft keyboard input.
  • flags Provides additional operating flags. Currently may be 0 or have the SHOW_IMPLICIT bit set.
SearchView searchView = (SearchView) findViewById(R.id.search);

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT);

3 comments :