Reputation: 29867
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" />
Upvotes: 4
Views: 8987
Reputation: 767
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;
}
}});
Upvotes: 9
Reputation: 5053
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>
Upvotes: 7
Reputation: 4981
By default it wraps new words if the line does not fit view width, but do not add carriage return to the text value. Though the user can manually enter '\n' symbol.
I think it would be convenient to allow user type whatever he wants and then just replace all of '\n' occurrences when you need to use EditText value.
Upvotes: -2
Reputation: 4910
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:lines="1"
android:paddingBottom="250dp"
android:text="You mean like this?"
android:textSize="24sp" />
Upvotes: -1