Restrict Edittext to Single Line

restrict edittext to single line

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

You forgot the android:maxLines attribute. And refer for android:inputType With your example, below will give this result:

<EditText 
android:id="@+id/searchbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:drawablePadding="10dp"
android:background="@drawable/edittext"
android:drawableLeft="@drawable/folder_full"
android:drawableRight="@drawable/search"
android:paddingLeft="15dp"
android:hint="search...">
</EditText>

EditText won't write into single line

I think this will work for you. Let me know if it works.

<EditText
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/colorBlue"
android:textAlignment="textEnd"
android:textSize="40sp"
android:inputType="text"
android:maxLines="1"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"/>

This post might help you as well.

Disable Enter in editText android

Because of different Android versions, the best practice is to include these parameters at the same time:

         android:maxLines="1"
android:lines="1"
android:singleLine="true"

If you don't, it will not work correctly on some devices.

Edittext View One line

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Full Name"
android:scrollHorizontally="false"
android:id="@+id/textView_data2"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:maxLines="1"
android:inputType="textPersonName"
android:gravity="fill_vertical" />

Unable to make EditText multi-line

Okay so, I knew that the problem arises when I click on the edit button. Clicking the button made the editText single lined automatically even though I had mentioned the InputType as Multi-line in XML very clearly.

What I did to make the editText multi-line after the button click is that I wrote editTextContent.setSingleLine(false); in my java file inside the onClick button code.
And that is it. :)

Prevent EditText from going to new line when 'Space' is tapped

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.



Related Topics



Leave a reply



Submit