Question

I use an EditText in my code and compare its content to a string when clicking on a button. Unfortunately doing this with the enter key through onKey causes problems because enter creates a line break. I used:

setSingleLine(true);

to prevent this. But now pressing enter leads to the EditText losing its focus. Why does it behave like this and how do I fix it?

Was it helpful?

Solution

Try using this android:lines="1" in your layout xml for EditText.

This will consider the Enter Key as new line and focus will not loose. Although existing text might not be visible due to only one line, as it moved upword and get hidden.

OTHER TIPS

Set the EditText as the next focus of itself:

<EditText
    android:id="@+id/input"
    ...
    android:imeOptions="actionNext"
    android:nextFocusForward="@+id/input"
    android:singleLine="true" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top