362

possible duplicate : android-singleline-true-not-working-for-edittext

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search...">
</EditText>

I want to make the above EditText to have only single line. Even if the user presses "enter" the cursor should not get down to the second line. Can anybody help me doing that?

Community
  • 1
  • 1
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86

26 Answers26

642

Use android:maxLines="1" and android:inputType="text"

You forgot the android:maxLines attribute. And refer for android:inputType With your example, below will give this result:

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:inputType="text"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search...">
</EditText>
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • 18
    @Lukasz'Severiaan'Grela in the current Eclipse IDE, no deprecation hint is shown, and android:maxLines does not restrict input to one line – mjn Jun 26 '13 at 08:09
  • @mjn, is it possible that you're compiling against an old SDK and thus not seeing the deprecation? – Brett Aug 16 '13 at 13:56
  • I'm using SwiftKey as input method and I'm having a problem with this. I manage in inserting `\n` characters with the keyboard, but they are displayed as spaces in the TextEdit. Any way to totally disable carriage returns? – Richard-Degenne May 14 '14 at 14:43
  • 2
    @Praveen: Can you please give the reference where it says the [android:singleLine](http://developer.android.com/reference/android/widget/TextView.html#attr_android:singleLine) is deprecated? I cannot see it there for API 23. I don't see it mentioned in the documentation and also I don't get any warning when I use it with `23.0.1` SDK – Shobhit Puri Sep 17 '15 at 22:31
  • 1
    @ShobhitPuri Its not deprecated. Also, it was an edit. I didn't mention deprecated. – Praveenkumar Sep 18 '15 at 07:15
  • 1
    Does not make single line when writing text if you don't specify inputType. Otherwise it just makes single line for the hint – Jemshit Oct 19 '15 at 07:37
  • Worked for me :) Anyway I was wondering why this piece of code didn't work for me?? android:maxLines="1" – Zohair Nov 25 '15 at 12:00
  • 5
    Deprecated since API level 3. See: http://developer.android.com/reference/android/R.attr.html#singleLine – Luis May 12 '16 at 16:40
  • This isn't working for me. If I set the edit text to have a text input type then the return key disappears in favor of a next button. – Doug Ray Nov 03 '16 at 00:12
  • maxLines="1", not working for me, it still accepts multiple lines, it just restricts the height of the edittext. – Narendra Singh Jan 20 '17 at 06:48
  • 4
    If you use only android:maxLines="1" will not work.So you need to use both android:inputType="text" and maxlines -1.Then it will work perfectly – Hanuman Mar 30 '17 at 06:56
  • @FrodeAkselsen Then, which one is correct? Can you point it out please. – Praveenkumar Jul 10 '17 at 05:01
  • @Praveenkumar if you set `android:maxLines="1"` you see only one line but if you hit `enter` you still go on a new line and that's **not** what OP wants. The wanted behavior can be achieved with `android:inputType="text"`, as described by @ralphspoon below. – Frode Akselsen Jul 10 '17 at 06:02
  • 1
    @FrodeAkselsen Even in my answer `android:inputType="text"` included. – Praveenkumar Jul 10 '17 at 06:16
  • right, now I see it, sorry, my bad! Maybe you want to point it out a bit more like you do for `android:maxLines="1"` – Frode Akselsen Jul 10 '17 at 06:21
  • 1
    @FrodeAkselsen I have pointed it out clearly. Please refer the updated answer. – Praveenkumar Jul 10 '17 at 06:23
222

Using android:singleLine="true" is deprecated.

Just add your input type and set maxline to 1 and everything will work fine

android:inputType="text"
android:maxLines="1"
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
ralphgabb
  • 10,298
  • 3
  • 47
  • 56
56

android:singleLine is now deprecated. From the documentation:

This constant was deprecated in API level 3.

This attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

So you could just set android:inputType="textEmailSubject" or any other value that matches the content of the field.

Community
  • 1
  • 1
Mr. Bungle
  • 1,696
  • 17
  • 21
18

You must add this line in your EditText in xml:

android:maxLines="1"
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
UGD
  • 363
  • 3
  • 4
16
android:maxLines="1"
android:inputType="text"

Add the above code to have a single line in EditText tag in your layout.

android:singleLine="true" is deprecated

This constant was deprecated in API level 3.

This attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

ChrisR
  • 607
  • 6
  • 16
13

Now android:singleLine attribute is deprecated. Please add these attributes to your EditText for an EditText to be single line.

android:inputType="text"
android:imeOptions="actionNext"
android:maxLines="1"
Kaushik
  • 6,150
  • 5
  • 39
  • 54
12

This will work for the EditText:

android:inputType="text"

Then I would set a max length for the input text:

android:maxLines="1"

Those are the 2 that are needed now.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
11

I have done this in the past with android:singleLine="true" and then adding a listener for the "enter" key:

((EditText)this.findViewById(R.id.mytext)).setOnKeyListener(new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
            //if the enter key was pressed, then hide the keyboard and do whatever needs doing.
            InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);

            //do what you need on your enter key press here

            return true;
        }

        return false;
    }
});
Aleks G
  • 56,435
  • 29
  • 168
  • 265
7

Include android:singleLine="true"

Vipul
  • 27,808
  • 7
  • 60
  • 75
6

Everyone's showing the XML way, except one person showed calling EditText's setMaxLines method. However, when I did that, it didn't work. One thing that did work for me was setting the input type.

EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);

This allows A-Z, a-z, 0-9, and special characters, but does not allow enter to be pressed. When you press enter, it'll go to the next GUI component, if applicable in your application.

You may also want to set the maximum number of characters that can be put into that EditText, or else it'll push whatever's to the right of it off the screen, or just start trailing off the screen itself. You can do this like this:

InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(8);
editText.setFilters(filters);

This sets the max characters to 8 in that EditText. Hope all this helps you.

Peter Griffin
  • 749
  • 9
  • 14
4

You must add this line in your EditText

android:maxLines="1"

and another thing, don't forget set android:inputType (whatever you want, text, phone .., but you must set it)

GianhTran
  • 3,443
  • 2
  • 22
  • 42
3

Programatically:

textView.setMaxLines(1);
Anton Duzenko
  • 2,366
  • 1
  • 21
  • 26
2

use

android:ellipsize="end"
android:maxLines="1"
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
2

In order to restrict you just need to set the single line option on "true".

android:singleLine="true"
user3469914
  • 79
  • 1
  • 1
1

Use Below Code instead of your code

<EditText 
    android:id="@+id/searchbox"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:inputType="text"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:layout_weight="1"
    android:layout_marginTop="2dp"
    android:drawablePadding="10dp"
    android:background="@drawable/edittext"
    android:drawableLeft="@drawable/folder_full"
    android:drawableRight="@drawable/search"
    android:paddingLeft="15dp"
    android:hint="search..."/>
Riot Goes Woof
  • 3,504
  • 5
  • 20
  • 35
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
1

The @Aleks G OnKeyListener() works really well, but I ran it from MainActivity and so had to modify it slightly:

EditText searchBox = (EditText) findViewById(R.id.searchbox);
searchBox.setOnKeyListener(new OnKeyListener() {

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
            //if the enter key was pressed, then hide the keyboard and do whatever needs doing.
            InputMethodManager imm = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(searchBox.getApplicationWindowToken(), 0);

            //do what you need on your enter key press here

            return true;
        }

        return false;
    }
});

I hope this helps anyone trying to do the same.

Mark Cramer
  • 2,614
  • 5
  • 33
  • 57
1

Also it's important to pay attention if the property android:inputType is textMultiLine. If so, the properties android:singleLine, android:imeOptions, android:ellipsize and android maxLines will be ignored and your EditText will accepted MultiLines anyway.

1

Add this line to your edittext

android:inputType="text"

Sagar Devanga
  • 2,815
  • 27
  • 30
1
android:imeOptions="actionDone"

worked for me.

bikram
  • 7,127
  • 2
  • 51
  • 63
1

please try this code

android:inputType="textPersonName"
0

As android:singleLine="true" is now Depreciated so use

android:maxLines="1"

Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

Maveňツ
  • 1
  • 12
  • 50
  • 89
0

in XML File add this properties in Edit Text

 android:maxLines="1"
yousef
  • 1,345
  • 1
  • 12
  • 20
0

At XML file. Just add

android:maxLines="1"

Kerelos
  • 83
  • 4
0

for me i do it like this , go to your textView in xml file and add this two lines

` android:maxLines="1"

android:inputType="text"`

jenos kon
  • 504
  • 4
  • 7
0

One more comment for all above. If you need the numerical input type just use inputType="number". But if you will use android:digits="0123456789" you canot achieve the effect you need with any of attributes as lines, maxLines, inputType

Sergey V.
  • 981
  • 2
  • 12
  • 24
0

android:singleLine="true" android:maxLines="1"

neelkanth_vyas
  • 192
  • 1
  • 7