Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android XML - EditText word-wrapping without newlines

I have this stupid and seemingly trivial problem with the properties of an EditText.

The properties I am trying to achieve for the EditText, are the following:

  • The contents of the view should NOT contain newlines. It can be text, numbers, symbols, but no newlines.

  • The soft keyboard should NOT display the enter button because of the above. It should instead display something like "Send" or "Done".

  • The contents of the view should NOT continue horizontally when reaching the edge of the screen. Instead I want to wrap the text, displaying it on multiple lines.

I have tried many different combinations, but I can not achieve this combination.

What I currently have is this, which is inside a RelativeLayout:

    <EditText
        android:id="@+id/comment_box"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_below="@id/preparation_text"
        android:hint="@string/comment_hint"
        android:inputType="textCapSentences|textAutoCorrect|text"
        android:scrollHorizontally="false"
        android:maxLength="400"
        android:imeOptions="actionSend"/>

It achieves 2 of 3. No newlines possible, keyboard displays "Send" rather than the enter-key for me, but the text continues on one line.

Changing inputType="text" to "textMultiLine" wraps text correctly on multiple lines, but also overrides the keyboard to always display the enter button.

I have tried different solutions around the Internet, including setting the properties maxLines="4", singleLine="true" and possible others that I have forgotten again.

I can not find a combination that works.

like image 574
Treeline Avatar asked Mar 09 '15 10:03

Treeline


Video Answer


1 Answers

Output:

enter image description here

Exp: The op is on right track. I did some research found that some options gets ignored which are specified in XML. but if the same options are set in code then it should do the trick. I used the same XML fragment specified in the question.

 <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:hint="hint"
        android:inputType="textCapSentences|textAutoCorrect|text"
        android:scrollHorizontally="false"
        android:maxLength="400"
        android:imeOptions="actionSend"
       />

And by adding the following lines in the code, it helped in achieving what you want.

 edit_text.setMaxLines(Integer.MAX_VALUE);
 edit_text.setHorizontallyScrolling(false);
like image 112
Rahul Khurana Avatar answered Sep 27 '22 19:09

Rahul Khurana