Android News Aggregator
Thursday February 9th 2012

Insider

Archives

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:

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);

And to hide:

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

Popular Posts That You Might Enjoy!




Read more @ AndroidGuys

Leave a Reply

You must be logged in to post a comment.