Friday, 5 April 2013

How to Programmatically Show and Hide Soft Keyboard in Android



In Androidsoft keyboard will be shown automatically when we launch an activity that contains a focusable EditText widget. But in some cases, we need to show or hide the soft keyboard manually or programatically. We can achieve this using InputMethodManager class.
For example, there is an EditText widget ‘nameEt’ inside an activity, to manually show and hide soft keyboard:
Show
?
1
2
3
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(nameEt, 0);
Hide
?
1
2
3
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(nameEt.getWindowToken(), 0);
Make sure to place those codes inside an activity.



No comments:

Post a Comment

Disqus for Android