Skip to content

Instantly share code, notes, and snippets.

@Dogesmith
Last active January 31, 2022 12:35
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dogesmith/2b98df97b4fca849ff94 to your computer and use it in GitHub Desktop.
Save Dogesmith/2b98df97b4fca849ff94 to your computer and use it in GitHub Desktop.
Android Multi-line EditText which prevents newline characters from being added

Using this code will allow you to set up a multi-line EditText where pressing the Return key on the virtual keyboard will remove focus from the EditText, thereby preventing the user from entering a newline character.

For mContent we set the raw input type as TYPE_CLASS_TEXT, IME Options as IME_ACTION_DONE to set up the Return key on the virtual keyboard as a DONE action. We also label the return key using setImeActionLabel.

The OnEditorActionListener lets us listen for actions on the keyboard. The giant nested if-statement is mostly empty because we only really need to check for a null event and IME_ACTION_DONE, since we set it earlier in the IME Options. I left the statements in to show you how it would look if you wanted to handle things differently.

NOTE: This has not been tested for hardware keyboards, but if you have tested it I would love to know if you found that it works.

mContent = (EditText) v.findViewById(R.id.dialog_item_content_EditText);
mContent.setText(mContentInit);
mContent.setRawInputType(InputType.TYPE_CLASS_TEXT);
mContent.setImeActionLabel(getResources().getString(R.string.done), EditorInfo.IME_ACTION_DONE);
mContent.setImeOptions(EditorInfo.IME_ACTION_DONE);
mContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event == null) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Capture soft enters in a singleLine EditText that is the last EditText
// This one is useful for the new list case, when there are no existing ListItems
mContent.clearFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
} else if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Capture soft enters in other singleLine EditTexts
} else if (actionId == EditorInfo.IME_ACTION_GO) {
} else {
// Let the system handle all other null KeyEvents
return false;
}
} else if (actionId == EditorInfo.IME_NULL) {
// Capture most soft enters in multi-line EditTexts and all hard enters;
// They supply a zero actionId and a valid keyEvent rather than
// a non-zero actionId and a null event like the previous cases.
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// We capture the event when the key is first pressed.
} else {
// We consume the event when the key is released.
return true;
}
} else {
// We let the system handle it when the listener is triggered by something that
// wasn't an enter.
return false;
}
return true;
}
});
<EditText
android:id="@+id/dialog_item_content_EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"/>
@amrinder007
Copy link

Hey, Thank you very much for this. Its exactly what i needed and it solved my problem. 👍

@nithindavid
Copy link

Saved my day 👍 thanks

@Mikelantonio
Copy link

thank you!

@sprengerst
Copy link

The | in the textview is not changing position accordingly with your solution.

@mxn21
Copy link

mxn21 commented Jun 24, 2020

Thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment