Android EditText: Done instead of Enter or Word Wrap instead of Multi Line

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.


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.


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);

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