Disable Enter in editText android

43,353

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" />
Share:
43,353
christian russo
Author by

christian russo

Updated on September 09, 2021

Comments

  • christian russo
    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.

    enter image description here

    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
    Aritra Roy over 8 years
    If your problem is solved, please upvote and accept it.
  • Aritra Roy
    Aritra Roy over 8 years
    Is there any problem with my answer that it is not accepted?
  • Kelevandos
    Kelevandos over 8 years
    It 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
    SMBiggs over 6 years
    Could you elaborate more? What are you trying to accomplish, and why does this solution work?
  • Muhammed Refaat
    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
    Reza Bigdeli over 5 years
    That is the correct answer! And also if you want to have multiline support use android:inputType="textMultiLine"
  • user924
    user924 about 5 years
    you can delete this answer
  • Tommy
    Tommy over 4 years
    Perfect! android:singleLine had been deprecated.
  • Phlip
    Phlip about 4 years
    These answers appear deprecated. What's imprecated is apparently a simple: android:inputType="text", or one of its derivatives: android:inputType="textCapWords". They clear the bit InputType.TYPE_TEXT_FLAG_MULTI_LINE, which is on by default.
  • Cyb3rKo
    Cyb3rKo over 3 years
    android:singleLine is deprecated now
  • Tigran Babajanyan
    Tigran Babajanyan over 3 years
    This doesn't disable enter button