1

We have a two edittext widgets one is limited to 1 line the other we are trying to limit it to 3 lines. The XML code posted below works for the limit to 1 line the limit to 3 lines fails. I have a less than elegant solution just use 3 single line edittext widgets. All of my research has pointed to using a key listener or a text watcher with further debate about keyboard types. Either way my ability level is not sure how to write or where to put that code for either of these supported solution. We would like to know how to solve this maxLines = 3 input issue. Best Link HERE

    <EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:id="@+id/etSecAnswer"
    android:layout_marginStart="140dp"
    android:layout_marginTop="230dp"
    android:textStyle="bold"
    android:textSize="16sp"
    android:maxLines="1"
    android:inputType="text"
    android:textColor="@color/color_Black"/>

<EditText
    android:layout_width="420dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:id="@+id/etNotes"
    android:layout_marginTop="260dp"
    android:layout_marginLeft="140dp"
    android:textStyle="bold"
    android:textSize="16sp"
    android:textColor="@color/color_Black"
    android:maxLines="3"
    android:inputType="text|textMultiLine" />
Community
  • 1
  • 1
James_Duh
  • 1,321
  • 11
  • 31
  • Correct me if I am wrong, the 3 line max works but you do not want the scrolling part of it? – Rod_Algonquin Dec 07 '16 at 19:57
  • @Rod_Algonquin The 3 line max does not work I can enter lines till the goats come home! as for the scrolling we made the EditText large enough to hold three lines we did try setting the scrolling to false with no results – James_Duh Dec 07 '16 at 20:08

4 Answers4

2

Below is a little code to getLineCount but the real question is what to do after you have the count? I am not a fan of the editable property it grays the view so that said what to do here is a little concept I discovered to LOCK and unLOCK the EditText give this a try

LOCK the EditText

    etNotes.setInputType(InputType.TYPE_NULL);
    etNotes.setFocusable(false);

unLOCK the EditText

    etNotes.setInputType(InputType.TYPE_CLASS_TEXT);
    etNotes.setFocusableInTouchMode(true);
    etNotes.setFocusable(true);
    etNotes.requestFocus();

Define the Listener in onCreate

    int L;
private void addTextChangedListener() {
    etNotes.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence q, int s, int c, int a) {
        }

        public void onTextChanged(CharSequence q, int s, int b, int c) {
            Log.d("TESTING", " LINES = " + etNotes.getLineCount());
            System.out.println("Line Count "+etNotes.getLineCount());

            L = etNotes.getLineCount();
            if(L > 3){
                etNotes.getText().delete(etNotes.getSelectionEnd() - 1,etNotes.getSelectionStart());
                //etNotes.append("\b");
                //L = 0;
                etNotes.setEnabled(false);

            }
        }
    });
}

Let us know how this works so others can benefit from the posted answer I have seen 9000 post that go back too far and current post on SO I only see about 15 with reasonable answers it would be nice to see the site below expand on this question CodePath Android Cliffnotes

Vector
  • 3,066
  • 5
  • 27
  • 54
  • I tried this and it has some odd results if we go beyond 3 lines the code will lock the etNotes but sending the backspace ctrl drives the app mad remove int L; L = etNotes.getLineCount(); if(L > 3){ etNotes.append("\b"); etNotes.setEnabled(false); } – James_Duh Dec 07 '16 at 21:32
  • @James_Duh You might want to write the Listener as a KeyCode listener if you plan on using etNotes.append("\b") you need a way to remove the listener or exit Let me work on a test app see you much later with results – Vector Dec 07 '16 at 21:55
  • @James_Duh I updated the Code for the Listener it removes the last line of code Try this and let us know if this works It was fine for my fast test app – Vector Dec 07 '16 at 22:05
  • That one line of code below the if(L>3) is pure magic it removes the blank text below Line 3 and for some reason the enabled is not making the EditText widget text look all gray GREAT – James_Duh Dec 07 '16 at 22:14
0

Yes, unfortunately maxLines actually corresponds to the outer height of the EditText(How many lines visible) and not the actual text inside the field (Limiting actual typed text).

You can however get the number of lines typed and //do something, like delete the last line without having to write/handle the three separate EditTexts

Get the count something like this:

if(yourText.getLayout().getLineCount() > lineLimit;){
    //do something like delete last
}
NSTuttle
  • 3,237
  • 2
  • 17
  • 26
0

Try below code

android:lines="3"

Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38
0

May be below code snippet can help you. Although it's in Kotlin, but you can consider the similar for your java code as well.

// we are checking for line count we need to keep and according to that we are defining inputType dynamically.
val lineCount = placeHolder.numberOfLines
if (lineCount > 1) {
    edtCaption.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
} else {
    edtCaption.inputType = InputType.TYPE_CLASS_TEXT
}

// we are setting flag of  single line false.
edtCaption.setSingleLine(false)

// here, we are deleting the text if it exceeds the max lines we defined.
edtCaption.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(charSequence: CharSequence, i: Int, i2: Int, i3: Int) {}
    override fun onTextChanged(charSequence: CharSequence, i: Int, i2: Int, i3: Int) {}

    override fun afterTextChanged(editable: Editable) {
        if (null != edtCaption.layout && edtCaption.layout.lineCount > lineCount) {
            edtCaption.text.delete(edtCaption.text.length - 1, edtCaption.text.length)
        }
    }
})

Conclusively, it will allow us to restrict user to input exact lines we think.

Jigar
  • 421
  • 2
  • 11