Search code examples
androidandroid-layouttextviewscrollviewandroid-scrollview

Put a TextView with a ScrollView between two other views (header and footer)


I would like to have a scrollable TextView / TextView inside a ScrollView.

I've already tried it myself and looked up multiple pages, however my issue always is that the text "text" TextView which says "some text" always exceeds the space between the two other Views despite getting into "ScrollMode"

Here's my XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="45dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:gravity="center_horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView"
    tools:targetApi="jelly_bean" />

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:layout_marginBottom="32dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintEnd_toEndOf="@+id/title"
    app:layout_constraintStart_toStartOf="@+id/title"
    app:layout_constraintTop_toBottomOf="@+id/title">

    <TextView
        android:id="@+id/middleText"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="some text"
        tools:targetApi="jelly_bean" />
</ScrollView>

<LinearLayout
    android:id="@+id/linearLayout">

... 

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Updated

enter image description here


Solution

  • Set this android:scrollbars = "vertical" property of your TextView also loosen the constraint property around your textview that's not appropriate. Here I have changed somewhat, which is making the textview scrollable:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="40dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginLeft="32dp"
            android:layout_marginTop="45dp"
            android:layout_marginEnd="32dp"
            android:layout_marginRight="32dp"
            android:gravity="center_horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView"
            tools:targetApi="jelly_bean" />
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="344dp"
            android:layout_height="50dp"
            android:layout_marginTop="32dp"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title">
    
            <TextView
                android:id="@+id/middleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"
                android:text="some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text some text"
                tools:targetApi="jelly_bean" />
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="32dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/scrollView"
            tools:ignore="MissingConstraints">
    
    
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>