Skip to content Skip to sidebar Skip to footer

Prevent Edittext From Going To New Line When 'space' Is Tapped

I want to have an EditText which is multi Line and First Letter of a Sentence to be capital. I'm using:

Solution 1:

Use android:maxLines="1" and android:inputType="text"

Your Edittext should look like below.

<EditText
        android:id="@+id/editText2"
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="32dp"
        android:layout_marginTop="67dp"
        android:background="#ddebed"
        android:ems="10"
        android:maxLines="1"
        android:inputType="text|textCapSentences"
        android:text="@string/same" />

OR

If you are looking for multilines you can edit your edittext as

android:inputType="text|textMultiLine|textCapSentences"

Similarly you can increase android:maxLines="2"

I have checked it out. You can use above code with some below modifications.

EditTexteditText= (EditText)findViewById(R.id.editText2);
editText.setSelection(0);

It will move your cursor to position 0. Also try to increase maxlines value. I have checked it my end and it's working fine.

Solution 2:

Try using this tag in xml.

android:singleLine="true"

Solution 3:

You can Add Two Hidden Spaces Before \n's. To add a Hidden space: Alt + 0160 (Numpad keys). So, When User is typing something, The Hidden Spaces will keep moving and UserText won't be able to reach to \n.

Post a Comment for "Prevent Edittext From Going To New Line When 'space' Is Tapped"