Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting EditText to single line makes it lose its focus after pressing enter

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?

like image 964
user3086884 Avatar asked Dec 10 '13 12:12

user3086884


People also ask

How do you make EditText not editable and clickable?

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java. et. setKeyListener(null); It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

How do you make EditText read only?

The most reliable way to do this is using UI. setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.

How do I edit EditText Uneditable?

If you want your disabled EditText to look the same as your enabled one, you should use android:enabled="false" in combination with android:textColor="..." . Show activity on this post. Used this if you have an icon to be clickable, this works for me.


1 Answers

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.

like image 192
umair.ali Avatar answered Oct 12 '22 03:10

umair.ali