Disable Enter in editText android
Solution 1
Because of different Android versions, the best practice is to include these parameters at the same time:
android:maxLines="1"
android:lines="1"
android:singleLine="true"
If you don't, it will not work correctly on some devices.
Solution 2
android:maxLines="1" allows the text to be multiline but limits the height of edittext to display a single line.
If you want to disable the enter (new line) key, set the input type to text
android:inputType="text"
Solution 3
Please use
android:singleLine="true"
instead of
<item name="android:maxLines">1</item>
<item name="android:minLines">1</item>
<item name="android:lines">1</item>
That single attribute is enough to make your EditText accept only one line of text and disable the enter button.
UPDATE
Don't worry about this comment,
android:singleLine is deprecated it's not a good practice using it
This attributed is officially supported and is not deprecated in any possible way. And it is also guaranteed that this will work in all devices and on all Android versions.
Solution 4
You may use
<EditText
......
android:imeOptions="actionNext" OR "actionDone" />
Solution 5
Use xml attribute
<EditText
......
android:inputType="text" />
christian russo
Updated on September 09, 2021Comments
-
christian russo over 2 years
I have an EditText but I want only one line. I put lime this
<item name="android:maxLines">1</item> <item name="android:minLines">1</item> <item name="android:lines">1</item>
but doesn't work.
See my code:
<LinearLayout style="@style/linearInputEntryText"> <LinearLayout style="@style/subLinearLayout"> <TextView style="@style/label" android:text="Last Name" android:id="@+id/textView2"/> <EditText style="@style/editTextEntryName" android:id="@+id/lastName"/> </LinearLayout> <View style="@style/linearInputEntryBlack" > </View> </LinearLayout>
and styles:
<style name="editTextEntryName" parent="editTextEntry"> <item name="android:maxLines">1</item> <item name="android:minLines">1</item> <item name="android:lines">1</item> <item name="android:layout_gravity">left</item> </style>
-
Aritra Roy over 8 yearsIf your problem is solved, please upvote and accept it.
-
Aritra Roy over 8 yearsIs there any problem with my answer that it is not accepted?
-
Kelevandos over 8 yearsIt is correct, but it is not true that the singleLine param should be used instead of the others you listed. They all should be used together or the TextView will behave incorrectly on selected devices. I have seen it firsthand, it is not a 'maybe' warning.
-
SMBiggs over 6 yearsCould you elaborate more? What are you trying to accomplish, and why does this solution work?
-
Muhammed Refaat about 6 years@AritraRoy now singleLine is working with me and max lines not, what is that I need to understand? a deprecated wtt is working and a functional one not. Do you have an idea about that ?
-
Reza Bigdeli over 5 yearsThat is the correct answer! And also if you want to have multiline support use
android:inputType="textMultiLine"
-
user924 about 5 yearsyou can delete this answer
-
Tommy over 4 yearsPerfect!
android:singleLine
had been deprecated. -
Phlip about 4 yearsThese answers appear deprecated. What's imprecated is apparently a simple:
android:inputType="text"
, or one of its derivatives:android:inputType="textCapWords"
. They clear the bitInputType.TYPE_TEXT_FLAG_MULTI_LINE
, which is on by default. -
Cyb3rKo over 3 yearsandroid:singleLine is deprecated now
-
Tigran Babajanyan over 3 yearsThis doesn't disable enter button