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!!!
Asked
Active
Viewed 655 times
2 Answers
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
-
Dear! I want that EditText must be single line not multi line and Q is `Why EditText is multi-line` – Arshad Ali Nov 10 '12 at 14:19
-
1@ArshadAliSoomro I mean't I did not read your question correctly at first. I thought you needed it to be multi-line. My mistake. – Anirudh Ramanathan Nov 10 '12 at 14:29
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