tasadar tasadar - 3 months ago 11
Android Question

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).

Answer

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>