If I set SingleLine=true on an EditText widget, I get a single-line edit control that doesn't allow hard returns to be inserted by the user (clicking Enter moves to the next field instead of inserting a new line). If I don't set SingleLine=true, the user can insert hard returns.
If I set layout_height="wrap_content", the EditText control will grow vertically to show all of the text. However, it only does this if SingleLine is not set to true.
So, my question is, is it possible to get the word-wrapping and vertical resizing, without allowing the user to enter hard line breaks? I guess I could trap the enter keypress, but then I would also have to catch other ways they might get one in there (copy/paste, not sure what else?). Is there a simple way to do this with just the right combination of properties?
I prefer the word-wrap where the user can see all of the text, compared to the horizontal scrolling of a single-line edit control, but I don't really want them thinking they can enter multiline text (and I don't want to have to support it). I guess I might just convert hard returns to spaces when I save the data to my database, if I have to (the legacy app I sync this data to on the PC can't handle hard returns).
I too was looking for something to do this as well. The only solution I found was to extend the EditText as follows:
package com.kylemilligan.test;
import android.content.Context;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;
public class NoNewlineEditText extends EditText
{
public NoNewlineEditText(Context context) {
super(context);
}
public NoNewlineEditText(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
{
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION;
if ((imeActions & EditorInfo.IME_ACTION_DONE) != 0)
{
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
}
if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0)
{
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}
}
And then in XML use like:
<com.kylemilligan.test.NoNewlineEditText
android:id="@+id/noNewLineText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:imeOptions="actionDone"
android:minLines="5" />
Hope this helps!
I was interested in this, and realized that the built-in Android messaging app has wrapped text without the enter key on the soft keyboard. According to the source layout here, it's done with the following options. Not all are necessary; I believe the key is to have textShortMessage
, textMultiLine
, and flagNoEnterAction
:
android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
android:imeOptions="actionSend|flagNoEnterAction"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With