Search code examples
androidkeypad

hiding the android keypad on pressing enter


I got a edit text and a save button , i want to close the keypad on clicking save button instead of pressing back key, keypad has to be closed after i enter save button. How to achieve this please help me and thanks in advance


Solution

  • You can override onkeypress on your edittext and check if the enter was pressed and if true then hide

    myEditText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) { 
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
            }
         return false;
        }
    });