3

I would like to create a text input that:

1) Always displays 3 lines

2) Does not allow the user to enter any more text than the available space in the 3 lines

3) Is not scrollable, if users enters text greater than 3 lines.

Technically, I allow for the user to enter up to 500 chars for saving to DB, but I am not expecting near this amount of text for the input. So, my usage of maxLength is only precautionary.

Here is my current EditText:

<EditText android:id="@+id/edit_description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lines="3"
    android:maxLines="3"
    android:maxLength="500"
    android:inputType="textMultiLine" />

Problem

The problem is that if the user enters text greater than 3 lines, the EditText adds additional lines and scrolls downward into the new line. It only limits the user to 500 characters.

Brice
  • 239
  • 5
  • 15
  • What's the question/problem? – nKn Apr 04 '14 at 17:51
  • Sorry, the problem is that if the user enters text greater than 3 lines, the EditText adds additional lines and scrolls downward into the new line. – Brice Apr 04 '14 at 17:52

2 Answers2

3

To control the lines introduced you can add a TextWatcher so anytime the user introduces a new value you can handle an error (for example delete the lines after the third one and show a toast). You can check new lines with \r and \n.

http://developer.android.com/reference/android/text/TextWatcher.html

TextWatcher watcher = new TextWatcher() {
    public void afterTextChanged(Editable s) {
      //here you can change edit text and show an error so the lines will never be 3
    }
...}
EditText editText=findById(...);
editText.addTextChangedListener(watcher);;
Carlos Verdes
  • 3,037
  • 22
  • 20
  • I believe this will work, but looking for something cleaner, preferably XML only. – Brice Apr 04 '14 at 18:09
  • 1
    I think there is no solution out of the box, but you can try any component on the web so instead of using EditText you can use another component (or actually make your own one). – Carlos Verdes Apr 04 '14 at 18:10
2

The attribute maxLines corresponds to the maximum height of the EditText, it controls the outer boundaries and not inner text lines. You'll have to control manually how many characters a user can input into the EditText.

Something like this might work:

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() > 3 )
                    return true;
                }

            return false;
        }
 });
CzarMatt
  • 1,773
  • 18
  • 22
  • I think this wouldn't work if the user paste a text, it's better my approach which tells you the final text introduced (not the key pressed). – Carlos Verdes Apr 04 '14 at 18:06
  • agreed with @Carlos on pasting text and also it doesn't matter which key. – Brice Apr 04 '14 at 18:08