Is it possible to have an EditText where the height of the text area is multiple lines but the text is actually just wrapped text but prohibits entering new lines. The only solution I can think of is allow the user to enter new lines but replace the new line character with just a space either on a keypress or after the text has been entered. Here is what I have:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:minLines="3"
android:textSize="24sp" />
The only thing that really worked for me in this case was to add an InputFilter that prevents a newline \n from being entered.
Code:
editText.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null) {
String s = source.toString();
if (s.contains("\n")) {
return s.replaceAll("\n", "");
}
}
return null;
}
}});
And the interesting lines from my configuration:
<EditText
...
android:singleLine="true"
android:inputType="text|textMultiLine"
android:imeOptions="actionDone"
/>
And if you want to hide and unfocus your EditText when pressing enter, replace the above code with this:
editText.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null) {
String s = source.toString();
if (s.contains("\n")) {
// Hide soft keyboard
InputMethodManager imm = (InputMethodManager)MainActivity.this.getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
// Lose focus
editText.clearFocus();
// Remove all newlines
return s.replaceAll("\n", "");
}
}
return null;
}
}});
This is what I am using in my The Rideshare App, and works great. My Target SDK is 17 (Android 4.0.3).
<EditText
android:inputType="text|textMultiLine|textCapSentences"
android:layout_margin="1dp"
android:gravity="top"
android:background="#FFEBCD"
android:textSize="26sp"
android:id="@+id/offer_notes"
android:padding="10dp"
android:textColor="#000"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="5sp"
android:hint="Notes, e.g. $10 per passenger, leaving early morning 7AM from Bay and Yonge intersection, no smoking or pets, please. Chevy Venture, very comfortable.">
</EditText>
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