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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With