11

I am working on Android. Previously i used onKeyListener to handle specific action on key event.

However, this way seems not to solve my problem since almost all key would get disable once i have implemented that listener to my EditText. After reading some topics in SO, i know that i should use TextWatcher instead, but i'm still wondering how to handle ENTER key event inside because parameters provided there are only CharSequence, Editable, etc. I didn't find any keyCode parameters.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
M Rijalul Kahfi
  • 1,460
  • 3
  • 22
  • 42
  • `However, this way seems not to solve my problem since almost all key would get disable once i have implemented that listener to my EditText.`..seams like your code is not correct. It should work. with `text watcher` you can not get `Enter event` – Mohsin Naeem Oct 14 '12 at 10:40
  • 1
    why was my question being down voted? – M Rijalul Kahfi Oct 16 '12 at 02:32

3 Answers3

18

try this

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (s.length()>0 && s.subSequence(s.length()-1, s.length()).toString().equalsIgnoreCase("\n")) {
        //enter pressed
    }
}
user2348959
  • 189
  • 1
  • 3
  • @user2348959 that method isn't fit for the purpose. If you move up a line with back space this also gets called. – DJ-DOO Sep 17 '15 at 12:50
  • @user2348959 it works if new line appears at the end of the string.. but it is not working new line appears in between the string – Pallavi Sep 05 '16 at 07:06
7

try this

protected View.OnKeyListener onEnter = new View.OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            //here do what you want
        }
        return false; // very important
    }
};
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
1

I think this is the better solution, because you can press 'Enter' in any place of your EditText field and not only at the end of line.

    edittext.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int st, int ct,
                                      int af) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() < 1 || start >= s.length() || start < 0)
                return;

            // If it was Enter
            if (s.subSequence(start, start + 1).toString().equalsIgnoreCase("\n")) {

                // Change text to show without '\n'
                String s_text = start > 0 ? s.subSequence(0, start).toString() : "";
                s_text += start < s.length() ? s.subSequence(start + 1, s.length()).toString() : "";
                edittext.setText(s_text);

                // Move cursor to the end of the line
                edittext.setSelection(s_text.length());
            }
        }
    });
Pavel Kataykin
  • 1,527
  • 15
  • 14
  • "It is an error to attempt to make changes to s from this callback." https://developer.android.com/reference/android/text/TextWatcher.html#onTextChanged(java.lang.CharSequence,%20int,%20int,%20int) – Brian Davis Dec 12 '18 at 16:15