[android] 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?

This question is related to android android-edittext

The answer is


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)


Programatically:

textView.setMaxLines(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).


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.


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

android:singleLine="true"

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.


Add this line to your edittext

android:inputType="text"


please try this code

android:inputType="textPersonName"

You must add this line in your EditText in xml:

android:maxLines="1"

use

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

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.


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

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"

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.


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


Include android:singleLine="true"


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.


in XML File add this properties in Edit Text

 android:maxLines="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..."/>

android:imeOptions="actionDone"

worked for me.


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"

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


At XML file. Just add

android:maxLines="1"