24

I want to make the EditText with max lines of 2 and max length of 20. If the EditText length is more than 10, it should automatically go to a new line so the user don't need to press Enter. Can someone help me with this requirement?

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
Frank Junior
  • 575
  • 1
  • 9
  • 19
  • 3
    I haven't tried but I think you should try with TextWatcher. When the length reaches 10, add "\n" to the current String. – DucTran Apr 17 '14 at 03:45

6 Answers6

53

ADD this to your EditText xml code

android:inputType="textMultiLine"

This will automatically moves your text to next line while entering data.

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
rkrohit
  • 647
  • 6
  • 8
  • 2
    this doesn't add \n chars to the string. just breaks the line in the EditText view, which isn't what the OP is asking for. – Yevgeny Simkin Jan 25 '17 at 08:15
27

It should exist a most elegant way but this solution might help you as a clue to achieve what you want. First of all, you will need to set your EditText values as below:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text|textMultiLine|textCapSentences"
    android:maxLength="21"
    android:gravity="left"
    android:maxLines="2" />  

You must to set the maxLength attribute to 21 because the enter char (new line) will take one char in the edittext, then the user will only can write 19 chars instead of 20.
Then, you should use a TextWatcher with a boolean (used if the user removes his previous chars), and this should be as follows:

// init global boolean
private boolean isReached = false;

// in onCreate method
edittext.addTextChangedListener(new TextWatcher(){
    @Override
    public void afterTextChanged(Editable s) {
        // if edittext has 10chars & this is not called yet, add new line
        if(textEd.getText().length() == 10 && !isReached) {
            textEd.append("\n");
            isReached = true;
        }
        // if edittext has less than 10chars & boolean has changed, reset
        if(textEd.getText().length() < 10 && isReached) isReached = false;
    }
});  

Note: However, you should be careful with this code. Indeed, the user can still pressed the Key Enter and then, add new lines. Maybe these answers might help you to handle it and keep the user only "on your road": Prevent enter key on EditText but still show the text as multi-line

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
4

Simply change the Input Type to textMultiLine

  <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine"/>
2

For the EditText to display 2 lines. You can write like this:

android:maxLength="20"
android:maxLines="2"
android:singleLine="false"
android:layout_width="Your Choice"
Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82
prakash
  • 109
  • 12
0

For the lines, do something like:

<EditText
    android:inputType="textMultiLine" <!-- Multiline input -->
    android:lines="2" <!-- Total Lines prior display -->
    android:minLines="1" <!-- Minimum lines -->
    android:gravity="top|left" <!-- Cursor Position -->
    android:maxLines="2" <!-- Maximum Lines -->
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"/>

you could also play with this parameter (Does NOT work for Android <= 3.0):

android:singleLine="false"

And for the number of characters, do something like:

EditText et= (EditText) findViewById(R.id.editText1);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(20); //Filter to 20 characters
et.setFilters(filters);

or, use this other parameter,

android:maxLength="20" in the xml

Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
  • thank you for your answer but i want automaticly enter when edittext length is more than 10, so basicly 10 character per lines and maxline is just 2 – Frank Junior Apr 17 '14 at 03:20
  • Just FYI **maxLines** attribute (when set > 2 lines) does not work in Android < 3... too! It's still set to 2 lines (whatever the value). – Blo Apr 17 '14 at 03:59
0

textView.setText("Your text"+"\n"+"Your text");