restrict edittext to single line
AndroidAndroid EdittextAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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>
Solution 2 - Android
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"
Solution 3 - Android
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.
Solution 4 - Android
You must add this line in your EditText in xml:
android:maxLines="1"
Solution 5 - Android
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).
Solution 6 - Android
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"
Solution 7 - Android
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;
}
});
Solution 8 - Android
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.
Solution 9 - Android
Include android:singleLine="true"
Solution 10 - Android
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.
Solution 11 - Android
Programatically:
textView.setMaxLines(1);
Solution 12 - Android
use
android:ellipsize="end"
android:maxLines="1"
Solution 13 - Android
In order to restrict you just need to set the single line option on "true".
android:singleLine="true"
Solution 14 - Android
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..."/>
Solution 15 - Android
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.
Solution 16 - Android
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.
Solution 17 - Android
Add this line to your edittext
android:inputType="text"
Solution 18 - Android
android:imeOptions="actionDone"
worked for me.
Solution 19 - Android
please try this code
android:inputType="textPersonName"
Solution 20 - Android
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)
Solution 21 - Android
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).
Solution 22 - Android
in XML File add this properties in Edit Text
android:maxLines="1"
Solution 23 - Android
At XML file. Just add
android:maxLines="1"
Solution 24 - Android
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"`