Search code examples
scrollandroid-edittext

How to scroll the content of an EditText by dragging the scrollbar?


I just noticed that the scrollbar only follows the content of the EditText. If you start dragging the scrollbar, the content will intercept the touch and will scroll on the opposite direction of the scrollbar:

example of scrolling an EditText

It works different in a ListView, where you can actually drag the scrollbar and scroll the content.

Another user pointed out this issue here, but I don't think he got the response he needed.

Anyone found a solution to this problem?

In case it's needed, this is the code for the example:

<EditText
        style="@style/customScrollbar" 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbarStyle="outsideOverlay"
 />

Solution

  • I ended up wrapping my EditText inside a ScrollView, this is more a workaround than a real solution:

       <ScrollView
            android:id="@+id/scrollview"
            style="@style/scrollbar_style"
            android:layout_width="200dp"
            android:layout_height="200dp">
    
            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
    
                android:background="@null"
                android:gravity="top"
                android:inputType="textMultiLine"
                >
    
                <requestFocus />
            </EditText>
        </ScrollView>
    

    This will let the touch drag the scrollbar.

    Edit: I have to say, this approach is 100 times better than using a native EditText. The EditText has a weird scroll, it's not as smooth as the ScrollView.