2

I want to allow use to enter just 5 lines, I tried this

<EditText
    android:layout_below="@+id/tv_signup_descriptionError"
    android:id="@+id/et_signup_description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:hint="@string/et_hint_enterDescription"
    android:singleLine="false"
    android:lines="5"
    android:gravity="top"
    android:scrollHorizontally="false"
    android:inputType="textMultiLine" 
    android:maxLines="5"/>

but still I can press Enter after the fifth line

michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
user user
  • 2,122
  • 6
  • 19
  • 24
  • Possible duplicate of [EditText maxLines not working - user can still input more lines than set](http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set) – CAMOBAP Feb 05 '16 at 07:51
  • Try this code http://stackoverflow.com/questions/8911638/set-maximum-number-of-text-lines-for-an-edittext – Vivekananda May 09 '16 at 10:54
  • You can limit the number of characters by adding android:maxLength. – Jeffrey Jun 27 '21 at 04:04

4 Answers4

6

@Swayam you are right, But this solution has improved :

1- better performance

2- in your solution if you entered 7 lines (your max input lines) then pressed enter in Top lines the last line will be delete, But in this solution "enter" key has ignored.

 mEditText.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER  && event.getAction() == KeyEvent.ACTION_DOWN) {

                    if ( ((EditText)v).getLineCount() >= 7 )
                        return true;
                    }

                return false;
            }
     });
ali safaei
  • 131
  • 1
  • 4
5

You cannot do that using any XML attributes.

maxlines represents the maximum height of the EditText and not the number of input lines.

You can however implement your own code to check for the number of lines.

The following is not my own code, but is taken from this answer.

mEditText.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            // if enter is pressed start calculating
            if (keyCode == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_UP) {

                // get EditText text
                String text = ((EditText) v).getText().toString();

                // find how many rows it cointains
                editTextRowCount = text.split("\\n").length;

                // user has input more than limited - lets do something
                // about that
                if (editTextRowCount >= 7) {

                    // find the last break
                    int lastBreakIndex = text.lastIndexOf("\n");

                    // compose new text
                    String newText = text.substring(0, lastBreakIndex);

                    // add new text - delete old one and append new one
                    // (append because I want the cursor to be at the end)
                    ((EditText) v).setText("");
                    ((EditText) v).append(newText);

                }
            }

            return false;
        }
Community
  • 1
  • 1
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • your code make the edittext to empty when i press the first enter , why ? – user user Feb 03 '13 at 12:14
  • `((EditText) v).setText("");` remove this part if you dont want that. – Swayam Feb 03 '13 at 12:18
  • sorry , your code doesn't work :(, i still can press `enter` after the fifth line – user user Feb 03 '13 at 12:18
  • Well, if you look closely in the code, you will see that the code was written for 7 lines and not 5 lines. Just change the `7` to `5` and I hope it will work. – Swayam Feb 03 '13 at 12:20
  • yes yes i know 7:) i press `enter` 80 times and still can, please would you correct the code? – user user Feb 03 '13 at 12:22
  • 1
    if you don't know , don't worry i can start new quesiton – user user Feb 03 '13 at 12:23
  • 1
    @FernadoMargin : I think that will be much appreciated. Because as of now, I really dont have the time to debug the code. I didn't even try it myself. Maybe I will have a look at it sometime later. Gotta rush now. Sorry. – Swayam Feb 03 '13 at 12:25
1

The maxLines attribute is to limit height of the EditText. To limit maximum number of lines you have to do it yourself in code.

msk
  • 8,885
  • 6
  • 41
  • 72
0

/** MAX_LINES You want to set */

  public void afterTextChanged(Editable editable) {
        int lines = mEditText.getLineCount();
        if (lines > MAX_LINES) {
            String s = editable.toString();
            int start = mEditText.getSelectionStart();
            int end = mEditText.getSelectionEnd();

            if (start == end && start < s.length() && start > 1) {
                s = s.substring(0, start - 1) + s.substring(start);
            } else {
                s = s.substring(0, s.length() - 1);
            }

            mEditText.setText(s);
            mEditText.setSelection(mEditText.getText().length());
            // Toast.makeText(RatingActivity.this, "max line set to MAX_LINES", Toast.LENGTH_SHORT).show();
  }

This piece of code works for me to set the EditText max lines limit to MAX_LINES.

JumSphenm
  • 21
  • 2