Gratzi
Gratzi

Reputation: 4693

Set maximum number of text lines for an EditText

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.

Upvotes: 16

Views: 33734

Answers (6)

Brinda Rathod
Brinda Rathod

Reputation: 2903

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

Upvotes: 1

NagRock
NagRock

Reputation: 325

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

Upvotes: 7

fvolodimir
fvolodimir

Reputation: 445

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.

Upvotes: 7

Jay Nair
Jay Nair

Reputation: 151

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

Upvotes: 0

Hounge
Hounge

Reputation: 85

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.

Upvotes: -3

MartijnG
MartijnG

Reputation: 825

You could use this:

android:singleLine="false" android:lines="5"

I don't know if that does what you need.

Upvotes: 12

Related Questions