Is there a way of specifying the maximum numbers of lines for an EditText
? By that I mean all the lines of text, not only the visible ones (as the android:maxLines
attribute is described). The lines number must not be 1, so android:singleLine
is not an option.
Asked
Active
Viewed 3.3k times
15
6 Answers
11
You could use this:
android:singleLine="false" android:lines="5"
I don't know if that does what you need.

MartijnG
- 815
- 4
- 12
- 24
-
5No. android:lines is about the visible lines. I'm interested in the text lines. If you set android:lines to an edit text and you write 10 lines, you will have 10 lines of text, but only 5 visible. What I want is to have maximum 5 lines of text. After the fift line you should not be able to write anymore. Thanks for the reply. – Gratzi Jan 18 '12 at 14:36
-
Okay, but there is no build in "row limiter". You have to do it by yourself. There's a topic on stackoverflow with a sample code, maybe you can use that: http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set – MartijnG Jan 18 '12 at 14:38
-
I saw that post, but the fix handles only the enter key and it's not very elegant. You delete portions of text, even if it isn't necessary to do so. – Gratzi Jan 18 '12 at 14:43
-
If you really want it you have to build a code yourself because there's no simple built in feature. Another option is to limit the editview to a max allowed characters with `android:maxLength="10"`. But then you can't check for max 5 lines. There's no other option for now I can think of, maybe someone else. Sorry. – MartijnG Jan 18 '12 at 14:48
-
`android:singleLine` is deprecated – Onkar Nene May 08 '17 at 13:01
7
set android:maxLines="2"in xml and add :
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
@Override
public void afterTextChanged(Editable editable) {
if (null != edittext.getLayout() && edittext.getLayout().getLineCount() > 2) {
edittext.getText().delete(edittext.getText().length() - 1, edittext.getText().length());
}
}
});
is solution for me . The same you can do for 3,4 ... n rows limit for your edittext.

Hugo Yates
- 2,081
- 2
- 26
- 24

fvolodimir
- 445
- 5
- 3
-
Can you format the code in a proper way and also explain it a little if you can? – giri-sh Aug 28 '15 at 10:13
-
maxLines="2" in your edittext in xml - makes only 2 rows visible for user , but he can type more then 2 rows . In this case 1 row will be hidden and user will see second and third row and so on.. In afterTextChanged(Editable editable) method we check - did new symbol appear in third row , or it is still in second or first. if symbol appears in third row - we delete it. So text can not be longer than 2 rows in edittext. – fvolodimir Aug 31 '15 at 08:55
7
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public class EditTextLinesLimiter implements TextWatcher {
private EditText editText;
private int maxLines;
private String lastValue = "";
public EditTextLinesLimiter(EditText editText, int maxLines) {
this.editText = editText;
this.maxLines = maxLines;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
lastValue = charSequence.toString();
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editText.getLineCount() > maxLines) {
int selectionStart = editText.getSelectionStart() - 1;
editText.setText(lastValue);
if (selectionStart >= editText.length()) {
selectionStart = editText.length();
}
editText.setSelection(selectionStart);
}
}
}
And then:
editText.addTextChangedListener(new EditTextLinesLimiter(editText, 2));

NagRock
- 316
- 4
- 15
1
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1" />
You just need to make sure you have the attribute "inputType" set. It doesn't work without this line.
android:inputType="text"

Brinda Rathod
- 2,693
- 1
- 20
- 32
0
I believe you can use this code to check the number of lines in edittext,
editText.getLayout().getLineCount() > row-limit;
Preferably you may use it in a TextWatcher Listener's "afterTextChanged()" to disallow the user for entering further if the row limit has been reached.
edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(editText.getLayout().getLineCount() > row-limit){
//Code to stop edittext from being edited any further.
}
});
-3
android:maxLength="140"
I believe this is equal to one line, in theory you could change the maxlength to "700" which would be 5 lines. I'm not exactly sure on the numbers but the logic behind it is valid.
-
1That's not valid. What if the user types 140x Enter? He creates 140 Lines then. – Sebi Jul 17 '16 at 15:05
-
Sebi- what the user enters doesn't create the maximum number of lines. this is the program code that sets the limit. – Hounge Dec 07 '16 at 09:58
-
This answer doesn't sound right to me. As @Sebi mention, 140 characters could span a large variety of lines, depending on which characters are in the string. Aside from line breaks, there is a large difference between the width of 140 'i's and 140 'm's. – Paul Wintz Aug 10 '17 at 20:10