15

I have a multiple line EditText that does not permit line returns. Right now I am replacing returns with some spaces as soon as they click save. Is there any way I can replace the on screen enter button with a Done button? (like it is for single line EditText)

I am aware that I should still strip out returns (\r\n|\r|\n) because the on screen keyboard is not the only way to add them.

Here is my current XML

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
          android:minLines="3" android:gravity="left|top"
          android:inputType="textMultiLine|textAutoCorrect|textCapSentences"
          android:imeOptions="actionDone" />
700 Software
  • 85,281
  • 83
  • 234
  • 341

4 Answers4

26

I suggest to read this article

http://savagelook.com/blog/android/android-quick-tip-edittext-with-done-button-that-closes-the-keyboard

really good example

XML:

<EditText 
    android:id="@+id/edittext_done"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter some text"
    android:imeOptions="actionDone"
    />

Custom Action Class:

class DoneOnEditorActionListener implements OnEditorActionListener {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            return true;  
        }
        return false;
    }
}

Activity Class:

public class SampleActivity extends Activity {    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_activity_layout); // sample_activity_layout contains our target EditText, target_edittext
 
        EditText targetEditText = (EditText)findViewById(R.id.target_edittext); 
        targetEditText.setOnEditorActionListener(new DoneOnEditorActionListener());
 
        // The rest of the onCreate() code
   }
}
Community
  • 1
  • 1
Stefano
  • 291
  • 3
  • 2
  • 1
    Since link-only answers are discouraged, I went ahead and edited in the relevant code. Thanks! – 700 Software Sep 12 '11 at 20:31
  • @GeorgeBailey why does the example focus on single line edit text when you automatically get the "next" or "done" functionality by default with a single line. as soon as you pipe in inputType textMultiLine, setting imeOption actionDone will not turn your enter key into the done button as far as i can tell. – topwik Sep 07 '12 at 14:18
  • @towpse, I don't know. I didn't use this solution. Perhaps you can formulate a question and get a better answer from someone who does know. If I was to guess I would say that imeOption actionDone only works if you properly combine that with the two sections of Java Class source code. Or perhaps you should read the linked article for further explanation. – 700 Software Sep 07 '12 at 17:59
  • @GeorgeBailey were you successfully able to modify the enter/action key for a multi line text box? i can only get the done button if i don't pipe in textMultiLine. – topwik Sep 07 '12 at 19:33
  • @towpse, I don't remember. It's been 6 months. I suggest you ask a new question, with the example code you have tried, and the current results, and a description of your desired results. – 700 Software Sep 10 '12 at 14:48
  • @GeorgeBailey welp, i just coded it that the enter button hides the keyboard and acts like a done button, this will work for now, thanks – topwik Sep 10 '12 at 15:44
  • That link goes to a disabled site. – Azurespot Feb 05 '15 at 03:21
20
android:inputType="textEmailAddress|textEmailSubject"

You need to set the input type as email address or email subject. Either one will give you your desired result. shouldAdvanceFocusOnEnter() is a private method in TextView which determines whether to enter a new line or move focus to next field.

700 Software
  • 85,281
  • 83
  • 234
  • 341
Veeresh
  • 1,052
  • 7
  • 12
  • 3
    does this actually work for multiline text? i can only get the done button if i don't pipe in textMultiLine. – topwik Sep 07 '12 at 14:13
5

If you're using android:inputType="textMultiLine|..." in your XML, or using the corresponding Java code:

editField.setInputType(
    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

then the only solution to show a ✔︎ Done or Search button is to follow the answers here:

Multiline EditText with Done SoftInput Action Label on 2.3

So you should extend EditText and override onCreateInputConnection() to manually set the IME_ACTION_xx flags; something like this...

@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;
}

This is because whenever you enable the "textMultiLine" option, it ignores any setting of android:imeOptions="actionDone" or android:imeActionLabel="actionDone", which is very strange and confusing.

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
4

I do this for multiline texts with an actionLabel:

editText.setSingleLine(true);
editText.setLines(10);
editText.setHorizontallyScrolling(false);
editText.setImeActionLabel(getString(R.string.ready), 0);
Damian
  • 219
  • 5
  • 14