5

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.

Treeline
  • 475
  • 1
  • 8
  • 23

4 Answers4

4

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);
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
1

textShortMessage is the inputType that you are looking for:

<EditText
        android:id="@+id/commentEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="100dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:gravity="left"
        android:text=""
        android:hint="hint"
        android:textSize="16dp"
        android:textColor="@android:color/black"
        android:lineSpacingExtra="0dp"
        android:lineSpacingMultiplier="1"
        android:background="@android:color/white"
        android:selectAllOnFocus="true"
        android:inputType="textShortMessage|textMultiLine|textCapSentences"
        android:singleLine="false" />

Refere to this to prevent paste function. To prevent pasting a new line.

Community
  • 1
  • 1
hasan
  • 23,815
  • 10
  • 63
  • 101
  • 1
    Your options for `singleLine` and `inputType` definitely changed something. The text now wraps properly, and there is no option to make newlines. It does however still seem to override the property for `imeOptions`. It now displays a smiley rather than "Done" or "Send". Is there anyway around that issue? I assume it is a property of using `textShortMessage`. – Treeline Mar 09 '15 at 10:24
  • In my app. I used this approach with Button for send(out of keyboard). – hasan Mar 09 '15 at 12:11
  • 1
    That is also my go-to solution, but not what I would prefer. – Treeline Mar 09 '15 at 12:26
0

Please use android:imeOptions="actionSend" to solve your problem.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
androminor
  • 322
  • 1
  • 13
  • @Ashvinsolanki This actually *does* suggest a solution. Whether it's correct, or high quality, I don't know - but it *is* an answer. – Paul Roub Aug 30 '19 at 21:12
0

I have the same issue a long time ago, you have to programmatically change Edittext property on OnCreate().

So in XML, create your Edittext like this

<EditText
       android:id="@+id/comment_box"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:minHeight="80dp"
       android:hint="Comment here"
       android:maxLength="400"
       android:inputType="textMultiLine" />

And on onCreate() (I wrote in kotlin not Java)

comment_box.imeOptions = EditorInfo.IME_ACTION_SEND
comment_box.setRawInputType(InputType.TYPE_CLASS_TEXT)
flx_h
  • 101
  • 3