Hide virtual keyboard when button is pressed

Is there a way to hide the virtual keyboard as soon as I press the button on Android? The keyboard initially appears when the user touches the edittext component; I would like it to close when the button is pressed.

+4
source share
3 answers

To hide the virtual keyboard, try / use this

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(medtSearchQuery.getWindowToken(), 0); 
+19
source

Best practice for hiding the keyboard:

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 

It will automatically get the current focus and hide the keyboard. It doesn't matter how many EditText views you have.

+16
source

Use code below

  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 } } }); 
+2
source

Source: https://habr.com/ru/post/1344354/


All Articles