1

I'm surprised that why EditText is multi-line in android i-e when we hit return key it goes to new line, and I also want to ask that can we add an event listener on pressing return key while working in EditText and not to go on new line. please help!!!

Arshad Ali
  • 3,082
  • 12
  • 56
  • 99

2 Answers2

2

You can use android:SingleLine

<EditText
    android:singleLine="true"
    ...
    ...

To detect an Enter key-press edittext.setOnKeyListener(new OnKeyListener() { ... }); can be used. (Reference)

Also, see this answer for more.

Community
  • 1
  • 1
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
0
edit.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        for (int i = s.length(); i > 0; i--) {
            if (s.subSequence(i - 1, i).toString().equals("\n"))
                s.replace(i - 1, i, "");
        }
    }
});
Lazy Beard
  • 305
  • 3
  • 10