Is there a way to hide the virtual keyboard once I click a button in android? The keyboard originally pops up when the user touches an edittext component; I'd like it to close once a button is pushed.
Asked
Active
Viewed 1.7k times
3 Answers
19
To hide virtual keyboard try/use this
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(medtSearchQuery.getWindowToken(), 0);
secretformula
- 6,256
- 2
- 30
- 53
Eby
- 3,059
- 4
- 20
- 31
-
1
-
@Eby - if we have more than one edit fields what to be given in place of "medtSearchQuery" – Siva K Oct 15 '12 at 07:18
-
1@SivaK Please check for my answer. If you will use that you do not need to worry about multiple EditText feilds. – Sanjay Joshi Dec 16 '13 at 11:54
17
Best Practice for hiding keyboard:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
It will automatically receive the current focus and will hide keyboard. Doesn't matter how many EditText
views you have.
Sanjay Joshi
- 1,542
- 6
- 27
- 44
2
Use Below Code
your_button_id.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
}
});
Prashant Maheshwari Andro
- 1,586
- 13
- 12