Alireza Mahfouzian
Alireza Mahfouzian

Reputation: 358

How to give editText max line programmatically

I've tried to programmatically change max line of edit text to a number like 3 and I've tried this line of code but it didn't work correctly:

binding.editText.maxLines = 3

Upvotes: 2

Views: 523

Answers (1)

Prashasth Nair
Prashasth Nair

Reputation: 272

The attribute maxLines corresponds to the maximum height of the EditText, it controls the outer boundaries and not inner text lines. If you want to change number of line use this code:

// set listeners
txtSpecialRequests.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        lastSpecialRequestsCursorPosition = txtSpecialRequests.getSelectionStart();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        txtSpecialRequests.removeTextChangedListener(this);

        if (txtSpecialRequests.getLineCount() > 3) {
            txtSpecialRequests.setText(specialRequests);
            txtSpecialRequests.setSelection(lastSpecialRequestsCursorPosition);
        }
        else
            specialRequests = txtSpecialRequests.getText().toString();

        txtSpecialRequests.addTextChangedListener(this);
    }
});

You can change the value of 3 in txtSpecialRequests.getLineCount() > 3.

Upvotes: 1

Related Questions