I fought too long, but finally found a solution!
Just create your own EditText class as such:
public class EditTextImeMultiline extends EditText { public void init() { addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { for (int i = s.length(); i > 0; i--) if (s.subSequence(i - 1, i).toString().equals("\n")) s.replace(i - 1, i, ""); } }); setSingleLine(); setHorizontallyScrolling(false); this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { EditTextImeMultiline.this.setLines(EditTextImeMultiline.this.getLineCount()); } }); } public EditTextImeMultiline(Context context) { super(context); init(); } public EditTextImeMultiline(Context context, AttributeSet attrs) { super(context, attrs); init(); } public EditTextImeMultiline(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public EditTextImeMultiline(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } }
This class removes lineBreaks (\ n), wraps the text like textMultiline, and allows you to replace the Enter button with ImeAction;).
You just need to call it in XML instead of the classic EditText class.
To explain the logic here:
- Set EditText as a singleLine to show the ImeAction button instead of Enter.
- Delete the horizontal scroll so that the text moves to the next line when it reaches the end of the view.
- View layout changes with onGlobalLayoutListener and set the line parameter to lineCount of the current text stored in the editText file. This is what refreshes its height.
Gabriel Morin Jan 13 '16 at 13:45 2016-01-13 13:45
source share