EditText will not wrap text

I know that there are many related questions in Stackoverflow, but I tried to do everything that was written in any of them, and so far I have not been able to get this to work.

My problem is simple. I have an Android app, and I have a field where the user will enter text. However, I do not want this text to be multi-line (it should not have "\ n" in it), but I want it to be completed if the line size is larger than the viewport. So basically, I want this to be the same behavior as the textBox in the Subject Gmail field.

This is my 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 behavior in is the one whose id is @ + id / edit_question_value. So far, I could either make it truly multi-line (so the user can enter input if he wants) or single-line, and the text will not be completed. Perhaps some elements around the text have some clutter, and so I turned on the whole view.

Does anyone know what is going on?

+7
source share
2 answers

This is what bothered many people before, and I could not find the answer when I needed it. In pure XML, this is so. If we can use programming, and not just markup, we can do it with a little hack if you can. Essentially, we declare a multiLine editText, and then intercept the return key to avoid new lines.

First declaration in xml :

 <EditText android:id="@+id/noReturnEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:imeOptions="actionDone" android:inputType="textMultiLine|textCapSentences" android:text="lorem ipsum \n dolor site amet helping stackoverflow" /> 

Yes, I know what you’re thinking: "Hey Juan, this is an EditText and I want one liner, as in the gmail domain." I know, I know, but we will take care of it right now by deleting the behavior of the multi-line part that we don’t need, while maintaining the wrap with the text we really want.

Now the rest of the code :

 EditText noReturnEditText = (EditText) findViewById(R.id.noReturnEditText); noReturnEditText.setOnKeyListener(new OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) return true; return false; } }); 

You can probably read the code quite easily, but in case someone needs an explanation, we set OnKeyListener to EditText we want it to be "oneline-multilinelike", and if the user presses the return key (KEYCODE_ENTER), we inform that the event is not propagated, returning true which in this context means that we processed the event.

Result:

enter image description here

Considerations

Pressing the return key is not the only way to enter a new line. The user can copy / paste some text and get a multi-line record in a single-line EditText. Do not worry! We can remove the value before saving it or using it with something like

myData = rawText.replace(System.getProperty("line.separator"), " ");

What if I have a lot of EditText views?

Well, you have two options. Do it one by one and stretch your eyes or do something like this by listing the views and looping around for each view.

 //Declare the reusable listener OnKeyListener myListener = new OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) return true; return false; } } //Loop through the views int viewIds[] = {R.id.edittext1,R.id.edittext2,R.id.edittext3,R.id.edittext4}; for(int id : viewIds) ((EditText)findViewById).setOnKeyListener(myListener); 
+8
source
 <EditText android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="center" android:background="@color/color_red" android:focusable="true" android:focusableInTouchMode="true" android:gravity="top|left" android:inputType="textMultiLine|textCapSentences" android:lines="10" android:maxWidth="200dp" android:maxLength="200" android:maxLines="10" android:singleLine="false" android:textColor="@android:color/white" android:textSize="26sp" /> 

try this. worked for me when I changed gravity to the top | left

0
source

Source: https://habr.com/ru/post/915858/


All Articles