Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide virtual keyboard when click a button

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.

like image 759
locoboy Avatar asked Nov 28 '22 00:11

locoboy


2 Answers

To hide virtual keyboard try/use this

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(medtSearchQuery.getWindowToken(), 0);
like image 136
Eby Avatar answered Nov 30 '22 23:11

Eby


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.

like image 33
Sanjay Joshi Avatar answered Nov 30 '22 22:11

Sanjay Joshi