41

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>
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
christian russo
  • 565
  • 1
  • 4
  • 10

10 Answers10

89

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"
Shivam
  • 1,086
  • 7
  • 8
57

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.

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
Kelevandos
  • 7,024
  • 2
  • 29
  • 46
  • 5
    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. – Phlip Feb 18 '20 at 23:15
  • This doesn't disable enter button – Tigran Babajanyan Jan 12 '21 at 17:10
25

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.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • 2
    If your problem is solved, please upvote and accept it. – Aritra Roy Aug 12 '15 at 12:39
  • Is there any problem with my answer that it is not accepted? – Aritra Roy Aug 12 '15 at 13:31
  • 1
    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. – Kelevandos Aug 12 '15 at 19:19
  • @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 ? – Muhammed Refaat Apr 03 '18 at 11:09
  • you can delete this answer – user924 Feb 26 '19 at 13:18
8

You may use

<EditText 
   ......
   android:imeOptions="actionNext" OR "actionDone" />
Abdul Rahman Majeed
  • 1,125
  • 2
  • 10
  • 22
5

Use xml attribute

<EditText
    ......
    android:inputType="text" />
Jorge Castro
  • 51
  • 1
  • 2
3

This worked for me,

by setting onKeyListener for the editText in my onCreateView -

editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            return (keyCode == KeyEvent.KEYCODE_ENTER);
        }
});

Edit: this may not work if you are using Android default keyboard

Roshan
  • 110
  • 2
  • 10
2

Use android:singleLine=true instead android:maxLines=1

Please read about singleLine

http://developer.android.com/reference/android/widget/TextView.html#attr_android:singleLine

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
2

Or if you wanna use java you could do this:

    edittext1.setSingleLine();
LukaxH
  • 71
  • 9
1

use android:singleLine="true" property

     <EditText 
       ......
       android:singleLine="true"
       android:id="@+id/lastName"/>
Rustam
  • 6,485
  • 1
  • 25
  • 25
-1

You can set the input type to textEmailSubject. I head the same issue and it solve it!

The Dude
  • 347
  • 4
  • 11