Question

Simply question - how to close virtual keyboard after hitting enter key/button?

I've tried this Handle “Enter” key on Jelly Bean and How to hide keyboard on enter key, but none of this works for me.

Was it helpful?

Solution

You have two options.

With xml:

 <EditText
            android:id="@+id/editText1"
            android:inputType="text"
            android:imeOptions="actionDone"/>

and with code.

edittext.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top