Java – Stop closing the keyboard when pressing Enter in EditText?

Stop closing the keyboard when pressing Enter in EditText?… here is a solution to the problem.

Stop closing the keyboard when pressing Enter in EditText?

I have an EditText with its singleLine property set to true. When I press Enter on the keyboard, the keyboard is hidden. Is it possible to avoid this?

Solution

I’ve been using OnKeyListener that is causing this issue. Switching to OnEditorActionListener prevents the keyboard from closing when I press Enter and gives me full control over it.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
                DO THINGS HERE
                return true;
            }
            return false;
        }
    });

Related Problems and Solutions