tasadar
tasadar

Reputation: 97

Horizontal text scroll inside EditText

I am making an EditText field with custom background, but the input text is going to be longer then the field itself. Also, text needs to be in one line.

My idea is to make text "move to the left" when the line reach the end of the field. I saw in some applications that this is possible.

I searched for answers but non of them respond to this specific problem. After I combined few of them I tried to solve problem in this way:

<EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text"
        android:textColor="#FFFFFF"
        android:paddingLeft="5dp"
        android:maxLines="1"
        android:maxLength="25"
        android:scrollHorizontally="true"/>

But, when I type text and It reaches the end of EditText field, it starts to stretch my field ("@drawable/edit_text" in particular) until I reach 25 characters.

Also, when I change layout_width to some specific value, text goes in 2nd row when reach end (even if maxLines is still 1).

Upvotes: 3

Views: 13038

Answers (4)

Meet Vora
Meet Vora

Reputation: 2818

After some research, I found it working.

Using android:inputType="text" inside EditText worked for me.

Hope this will help someone and will save your time :)

Upvotes: 16

Manesidlapy
Manesidlapy

Reputation: 39

It worked after adding

android:scrollHorizontally="true"
android:singleLine="true"
android:gravity="right|center_vertical" 

In

<EditText
        android:background="@drawable/sk"
        android:id="@+id/ed"
        android:textSize="@dimen/sed"
        android:textColor="@color/tc"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:gravity="right|center_vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

to my edittext

Upvotes: 2

jcaruso
jcaruso

Reputation: 2494

Put your EditText inside the HorizontalScrollView and it works..

            <HorizontalScrollView
                android:id="@+id/rlEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollHorizontally="true"
                android:layout_alignLeft="@+id/etPhone"
                android:layout_alignRight="@+id/imgBtnEdit"
                android:layout_below="@+id/etPhone" >
                <EditText
                    android:id="@+id/etEmail"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:ems="10"
                    android:enabled="false"
                    android:focusable="false"
                    android:text="[email protected]"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:scrollHorizontally="true" />
            </HorizontalScrollView>

Upvotes: 3

tasadar
tasadar

Reputation: 97

I got it. The point is that you need to put EditText inside RelativeLayout. Now It's working.

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text">
        <EditText
            android:layout_width="120dp"
            android:layout_height="21dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="6dp"
            android:background="#00000000"
            android:imeOptions="actionDone"
            android:inputType="textPersonName"
            android:maxLength="20"
            android:maxLines="1"
            android:paddingLeft="2dp"
            android:textColor="#FFFFFF"/>
    </RelativeLayout>

Upvotes: 3

Related Questions