Pankaj Singhal
Pankaj Singhal

Reputation: 16053

Restrict EditText to single line

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?

Upvotes: 366

Views: 300674

Answers (27)

jenos kon
jenos kon

Reputation: 564

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"`

Upvotes: 0

Kerelos
Kerelos

Reputation: 103

At XML file. Just add

android:maxLines="1"

Upvotes: 0

ΓDΛ
ΓDΛ

Reputation: 11110

For compose

TextField(
    //..
    maxLines = 1)

or

TextField(
    //..
    singleLine = true)

Upvotes: 0

neelkanth_vyas
neelkanth_vyas

Reputation: 240

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

Upvotes: 0

Sergey V.
Sergey V.

Reputation: 1028

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

Upvotes: 0

GianhTran
GianhTran

Reputation: 3711

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)

Upvotes: 5

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

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..."/>

Upvotes: 1

Trần Thị Diệu My
Trần Thị Diệu My

Reputation: 1423

please try this code

android:inputType="textPersonName"

Upvotes: 1

Ray Hunter
Ray Hunter

Reputation: 15557

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.

Upvotes: 13

bikram
bikram

Reputation: 7985

android:imeOptions="actionDone"

worked for me.

Upvotes: 1

yousef
yousef

Reputation: 1363

in XML File add this properties in Edit Text

 android:maxLines="1"

Upvotes: 0

Praveenkumar
Praveenkumar

Reputation: 24506

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>

Upvotes: 650

ralphgabb
ralphgabb

Reputation: 10538

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"

Upvotes: 227

Maveňツ
Maveňツ

Reputation: 1

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).

Upvotes: 0

UGD
UGD

Reputation: 363

You must add this line in your EditText in xml:

android:maxLines="1"

Upvotes: 18

Kaushik
Kaushik

Reputation: 6162

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"

Upvotes: 13

ChrisR
ChrisR

Reputation: 607

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).

Upvotes: 16

Raghu Nagaraju
Raghu Nagaraju

Reputation: 3288

use

android:ellipsize="end"
android:maxLines="1"

Upvotes: 3

Peter Griffin
Peter Griffin

Reputation: 789

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.

Upvotes: 7

Anton Duzenko
Anton Duzenko

Reputation: 2616

Programatically:

textView.setMaxLines(1);

Upvotes: 3

Sagar Devanga
Sagar Devanga

Reputation: 2819

Add this line to your edittext

android:inputType="text"

Upvotes: 1

Thales Valias
Thales Valias

Reputation: 61

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.

Upvotes: 2

user3469914
user3469914

Reputation: 79

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

android:singleLine="true"

Upvotes: 2

Mark Cramer
Mark Cramer

Reputation: 2854

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.

Upvotes: 1

Peter Horsley
Peter Horsley

Reputation: 1756

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.

Upvotes: 56

Aleks G
Aleks G

Reputation: 57346

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;
    }
});

Upvotes: 11

Vipul
Vipul

Reputation: 28103

Include android:singleLine="true"

Upvotes: 7

Related Questions