Momin Khan
Momin Khan

Reputation: 157

KeyEvent.KEYCODE_ENTER having \n causes two new lines

I am using Edittext and I have KEYCODE_ENTER listener for it.

     emojiconEditText.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
           if ((keyCode == KeyEvent.KEYCODE_ENTER)) {
            emojiconEditText.append("\n");
            return true;
            }
            return false;
        }
    });

Here "\n" is causing two newlines like "\n" + "\n". But I want only one newline. I am using input type = text multi line.

Upvotes: 1

Views: 1476

Answers (1)

bongo
bongo

Reputation: 763

This key event is fired two times. First when user push down enter, second when user push up enter. So You must add some additional assertion like this.

 event.getAction() == KeyEvent.ACTION_DOWN

Upvotes: 2

Related Questions