I know there are lots of related questions in Stackoverflow, but I tried to do everything that's written in any of them and so far I couldn't get this working.
My problem is simple. I have an android application, and I have a box where the user will input text. However, I don't want this text to be multiline (it shouldn't have "\n" in it), but I want it to wrap if the size of the line is greater than the viewport. So basically, what I want is the same behaviour that the textBox on Gmail Subject field has.
This is my whole view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question"
android:textAppearance="@style/titleFont"/>
<EditText
android:id="@+id/edit_question_value"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:gravity="top"
android:inputType="textImeMultiLine|textEmailSubject"
android:maxLength="70"
>
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/answer"
android:textAppearance="@style/titleFont"/>
<EditText
android:id="@+id/edit_question_answer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="top"
android:layout_weight="0.7"
android:inputType="textMultiLine"/>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:id="@+id/save_question"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.5"
android:text="@string/save"/>
<Button
android:id="@+id/cancel_edit_question"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/cancel"/>
</LinearLayout>
The text I want to have this behaviour is theone with the id equals to @+id/edit_question_value. So far, I have been able to either make it truly multiline (so the user can type enter if he wants) or single-line and the text won't wrap. Perhaps some of the elements around the text are having some interference, and that's why I included the whole view.
Does anyone knows what is happening?
This is something that has bothered a lot of people before and I couldn't find an answer when I needed it. In pure XML, that is. If we can use programming and not just markup, we can do this with a little hack if you may. Basically, we are declaring a multiLine editText and then catching the return key to avoid new lines.
First the declaration in xml:
Yes, I know what you're thinking "But hey, Juan this is a multiliner
EditText
and I want a one liner like on the subject area of gmail". I know, I know, but we are going to take care of it right now, by removing the behavior of the multiline part we don't want, while keeping the text wrapping we do want.Now the rest of the code:
You can probably read the code quite easily, but in case someone needs an explanation, we are setting an
OnKeyListener
to theEditText
we want to be "oneline-multilinelike" and if the user presses the return key (KEYCODE_ENTER) we tell the event not to propagate by returningtrue
which in this context means we've handled the event.Result:
Considerations
Pressing the return key is not the only way of entering a new line. The user could copy/paste some text and end up with a multi-line entry on a single-line EditText. Worry not! We can strip the value before we store it or use it with something like
myData = rawText.replace(System.getProperty("line.separator"), " ");
What if I have lots of
EditText
views?Well, you basically have two options. Do it one by one and pull your eyes out or do something like this, its listing the views and looping for each view.