Home » Android » Android EditText Multiline does not work as it should

Android EditText Multiline does not work as it should

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m pretty desperate about this feature.
I tried pretty much everything there is to find to made these EditTexts multiline enabled,
but they just keep going on a single line scrolling the entire EditText with it.

How hard can it be to stop at the end of the border of the EditText and move to the next line?

I have this activity with an EditText and 2 buttons. One of these buttons adds a predetermined line of text to the EditText. The other puts the EditText’s text into some form of object that I use later in the app.

However I can’t get this multiline feature to work.. I’ve tried limiting the size. Setting the multiline flag. Disabling singleline. Giving lines, and minLines a random number (10).
Disabling horizontalscroll on the EditText. But nothing works….

Can anyone tell me what the hell I’m doing wrong? And how I can fix this horrid abomination of an EditText.

This is how my nightmare looks like now.

    <EditText
        android:id="@+id/callofedittext"
        android:layout_width="wrap_content"
        android:inputType="textMultiLine"
        android:width="300dp"
        android:minLines="10"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:textColor="@color/textWhite"
        android:background="@color/textBlack"
        android:paddingLeft="3dp"
        android:singleLine="false"
        android:scrollHorizontally="false"

        >

        <requestFocus />
    </EditText>

It haunts my dreams…

EDIT: > Light at the end of the tunnel.

While I was focussing on the xml.. A new clean project pointed out to me that EditText textMessage = (EditText)findViewById(R.id.callofedittext); textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); is causing all of my problems. Not specifically the properties inside the xml.

How to&Answers:

From this comment, the inputType was set in the code as well with:

textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE |
                         InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

This is actually not correct, because TYPE_TEXT_FLAG_MULTI_LINE and TYPE_TEXT_FLAG_CAP_SENTENCES are only flags, and do not contain the actual input type. In order for them to work, they must be layered as flags on top of InputType.TYPE_CLASS_TEXT. Without this type class flag, the edit text does not have a base input type class to apply your flags to, and defaults to no specified input type.

So, the correct way to set the input type with both of these flags is:

textMessage.setInputType(InputType.TYPE_CLASS_TEXT |
                         InputType.TYPE_TEXT_FLAG_MULTI_LINE |
                         InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

For official details on how these flags work, see the Android Developer Docs on InputType and TextView – android:inputType

I’m not sure why the design decision is this. Personally, I think they should have hidden how they are representing their flags (as ints/bit flags), and instead had enums and/or subclasses of InputType for their public interface.

Answer:

hey you have to add the following code in xml file ..

android:gravity="top"
android:maxLines="4"
android:inputType="textMultiLine"
android:scrollbars="vertical"
android:textSize="16sp"
android:padding="10dp"

and you have to put activity file …

edtComment = (EditText) findViewById(R.id.edtComment);
edtComment.setMovementMethod(new ScrollingMovementMethod());

this is works for me and hope it will works for you …..

Answer:

Have you tried using

android:layout_height="wrap content"

instead of

android:layout_height="match_parent"