0

I need an EditText with vertical and horizontal scrolling and a fixed size, I used ScrollView and HorizontalScrollView but the EditText does not fill the View above, leaving just a small square that only fills when the text is added.

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="start"
            android:scrollbars="vertical|horizontal"
            android:inputType="textMultiLine"
            android:padding="10dp"
            android:background="@color/purple_700"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
            
    </HorizontalScrollView>
</ScrollView>

I've already tried changing the layout_width and layout_height to fixed and EditText's behavior remains the same.

1
  • Why would you put an EditText in a scroll view? Could you post a picture of your UI, or something? This seems a little bizzare Commented Jun 1, 2024 at 4:40

1 Answer 1

0

To create an EditText with both vertical and horizontal scrolling in a fixed-size container in Android, you need to wrap it in both HorizontalScrollView and ScrollView. Additionally, ensure that the EditText fills the parent view properly.

Set a fixed size for the parent container. Wrap the EditText inside both ScrollView and HorizontalScrollView. Ensure the EditText has the correct layout parameters to fill its container. Here's an example layout XML that demonstrates this setup:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

<FrameLayout
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:background="#ddd">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fillViewport="true">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#fff"
                android:padding="10dp"
                android:scrollHorizontally="true"
                android:hint="Enter your text here..." />

        </ScrollView>
    </HorizontalScrollView>
</FrameLayout>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It didn't work exactly as I planned but with some adjustments it worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.