October 1, 2014

How to Show/Hide Soft Keyboard Programmatically [DEV TIPS & TOOLS]

how_to_dev_tip_wrenchI had to figure it out today so I thought I’ll shared it with the world. Say for whatever reason you need to show or to hide virtual (soft) keyboard in your Android app. It’s not very intuitive but can be easily done. Here’s one scenario – you are executing action that places focus into the text field. To my surprise it doesn’t bring up a soft keyboard on MyTouch (or emulator). To actually do so requires the following additional code:

To show:

[java]
EditText editText = (EditText) findViewById(R.id.myEdit);
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
[/java]

And to hide:

[java]
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
[/java]

Deals, Discounts, Freebies, and More! Click here to save today!