Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText Multiline does not work as it should

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.

like image 699
Totumus Maximus Avatar asked Aug 13 '14 19:08

Totumus Maximus


People also ask

How to enable multiline in EditText Android?

Just add this android:inputType="textMultiLine" in XML file on relevant field. Save this answer.

How to display multiline text in Android?

For this you have to declare a line of code into layout file 'android:text="@string/text"' and go to the \\app\src\main\res\values\strings. xml and add a line of code ' Your multiline text here' Also use '\n' to break the line from any point else the line will automatically be adjusted.


1 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.

like image 57
Trevor Siemens Avatar answered Oct 13 '22 00:10

Trevor Siemens