13

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.

Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • Have you tried setting android:inputType="textMultiline" ? – Trevor Siemens Aug 13 '14 at 20:28
  • Yes I have. I will add it to the example for clarification. – Totumus Maximus Aug 13 '14 at 21:19
  • If you hit enter on the keyboard, will it let you go to the next line? If that works, it might be your layout_width="wrap_content". If you try setting that to fill_parent, or a specific dp value, it could force the line wrap. Your use of android:width looks incorrect. http://stackoverflow.com/questions/11181971/difference-between-androidwidth-and-and-androidlayout-width – Trevor Siemens Aug 13 '14 at 21:22
  • When i press the enter button the keyboard disappears and nothing further happens. Changing wrap_content to any of the other options didn't help. Also the usage of width is to make sure we have an exact width of the EditText (or any other element for that matter). None of these work even though it would be logical for them to work in some way. – Totumus Maximus Aug 13 '14 at 21:34
  • I'm a bit at a loss then. Perhaps you should remove all the properties you can, to see if it works as expected. If so, re-introduce them. – Trevor Siemens Aug 13 '14 at 21:44
  • Welcome to the nightmare ;) I just don't get why it doesn't work. It just puts all text on a single line and scrolls infinitely to the right in the process.. Whatever layout I apply, whatever property I add/remove/edit. – Totumus Maximus Aug 13 '14 at 21:49
  • I did start a new project just now. With just an EditText and started with a clean slate.. Somehow I got it to work. I'm now introducing new elements that are similar to the ones I need for the real project. – Totumus Maximus Aug 13 '14 at 22:02
  • Keep me posted, and submit an answer for your question if you figure it out please. – Trevor Siemens Aug 13 '14 at 22:04
  • 1
    Ok, I figured out on way to screw with the EditTexts. I wanted to capitalise every sentence inside this multiline EditText (which I think is a pretty reasonable request). And I read somewhere I should add the flags programmatically like this to have both multilines and Capitalisation. "EditText textMessage = (EditText)findViewById(R.id.callofedittext); textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);" But aparantly this screws up everything.... Why is capitalisation not just standard for EditTexts? – Totumus Maximus Aug 13 '14 at 22:10
  • Not sure, however, your programmatic setInputType is incorrect. You are missing the text type class. Try textMessage.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); – Trevor Siemens Aug 13 '14 at 22:14
  • Man.... that works.... How did you come up with this? – Totumus Maximus Aug 13 '14 at 22:23
  • I literally just had that problem 2 days ago, and was bashing my head for a while. I'll point a full answer/explanation. – Trevor Siemens Aug 13 '14 at 22:24
  • You have my gratitude. I edited my question to fit my problem more. I kinda tried to fix this for a couple of weeks now (since its just a side-project of my own) and couldn't get it done. I look forward to reading why this extra flag is needed. – Totumus Maximus Aug 13 '14 at 22:29

3 Answers3

35

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.

Community
  • 1
  • 1
Trevor Siemens
  • 629
  • 5
  • 10
  • Thanks again man. Do you have some official documentation on this solution maybe? From the 'developer.android.com' preferably? This might help people in the future. – Totumus Maximus Aug 13 '14 at 22:38
  • This is what solved my problem. I wasn't looking down this avenue because `InputType.TYPE_TEXT_FLAG_CAP_SENTENCES` was giving me the expected behaviour without setting `InputType.TYPE_CLASS_TEXT`. Luckily I stumbled on this and it's all sorted now. – user2590928 Mar 02 '17 at 02:50
  • This works for text but the error message is in the center. How to align the error message to the right top of the Edittext? – Shyamaly Lakhadive Apr 10 '21 at 05:20
  • this does NOT work for me. I'm using API 30. specifically it doesn't automatically add a new line when the text hits the width boundary. the stupid thing just lets you keep typing but you don't see what the hell you are typing....but it is adding it alright to the textview. – driftwood Jan 08 '22 at 21:29
10

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

Sachin
  • 528
  • 5
  • 17
1

Have you tried using

android:layout_height="wrap content"

instead of

android:layout_height="match_parent"
user1282637
  • 1,827
  • 5
  • 27
  • 56