Asha jyothi
Asha jyothi

Reputation: 11

Overcoming \n with in edittext

I am working on edit text, by entering some text then go to enter key on the soft keyboard it will go to next line again pressing enter it will go to the third line finally the resultant text of edit text should be consists of three line with spaces.

How can Ii get that result in a single line?

Any help would be appreciated.

Upvotes: 1

Views: 1098

Answers (3)

Abin Antony
Abin Antony

Reputation: 119

Make android:singleLine="true" in xml file is the proper way.

  <EditText
     android:id="@+id/editText1_1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:ems="10" > 

But still, if you want to type multiple line in edittext and print it(display it) in a single line do as below(it is not the perfered way, but you need so do it like below)

String editTextVal =(EditText)findViewById(R.id.editText1_1)).getText().toString();
Log.d("in single Line", Html.fromHtml(editTextVal.replace("\n", " ")).toString());

Try this.

Note : Remove android:singleLine="true" from xml for muliple line.

Upvotes: 1

KishuDroid
KishuDroid

Reputation: 5451

If you want to make your EditText to restrict to Single Line then Use below line:

1) Include android:singleLine="true" to your EditText.

And If you want to get EditText's value OnKeyBoard click then do following:

2) Use editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

OR

3) Include android:imeOptions="actionDone" to your EditText.

Upvotes: 0

Suhas Bachewar
Suhas Bachewar

Reputation: 1230

<EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:padding="5dp"
        android:hint="Add text here"
        android:gravity="left|center"
        android:singleLine="true"/>

Upvotes: 0

Related Questions