Search code examples
androidandroid-constraintlayoutandroid-scrollview

ScrollView not working inside constraint layout


I have a scrollview inside ConstraintLayout. but scrollview not working in ConstraintLayout. I tried NestedScrollView instead of ScrollView but it still not working. ScrollView worked fine with LinearLayout or RelativeLayout but in didn't work in ConstraintLayout.I changed android:layout_height to match_parent and wrap_content but it didn't work. what is the problem?

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <include
        android:id="@+id/other_toolbar_xml"
        layout="@layout/other_toolbar_xml"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml"
        android:fillViewport="true"
        tools:ignore="MissingConstraints"
        >

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <ImageView
                android:id="@+id/img_content_xml"
                android:layout_width="match_parent"
                android:layout_height="170dp"
                app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml"
                android:scaleType="fitXY"
                tools:ignore="NotSibling"
                />

            <TextView
                android:id="@+id/title_content_xml"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/img_content_xml"
                android:layout_marginRight="16dp"
                android:paddingLeft="16dp"
                android:textDirection="rtl"
                android:text="title"
                android:textSize="17sp"
                android:textColor="#1d1d1d"
                />

            <TextView
                android:id="@+id/content_content_xml"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/title_content_xml"
                android:layout_marginTop="20dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"
                android:paddingLeft="16dp"
                android:textDirection="rtl"
                android:text="content"
                android:textColor="#1d1d1d"
                />

            <ImageView
                android:id="@+id/img_date_content_Xml"
                android:layout_width="18dp"
                android:layout_height="18dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/content_content_xml"
                android:layout_marginTop="20dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                android:src="@drawable/date"
                />

            <TextView
                android:id="@+id/date_content_xml"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/content_content_xml"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintRight_toLeftOf="@id/img_date_content_Xml"
                android:layout_marginTop="20dp"
                android:layout_marginRight="8dp"
                android:layout_marginEnd="8dp"
                android:text="date"
                android:textColor="#1d1d1d"
                android:layout_marginBottom="16dp"
                />

            <TextView
                android:id="@+id/subject_content_xml"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/content_content_xml"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginTop="20dp"
                android:layout_marginStart="16dp"
                android:layout_marginLeft="16dp"
                android:singleLine="true"
                android:text="subject"
                android:textColor="#1d1d1d"
                />

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

Solution

  • You missed some constraint to give as you have added tools:ignore="MissingConstraints" in <ScrollView tag.

    There are two ways:

    1. Remove Parent Constraint Layout and use RelativeLayout as no need of ConstraintLayout in just two layouts. (It is mostly used for complex view to make it easy)

    2. Give proper constraint if you want to use ConstraintLayout. You missed left, right, bottom constraint as follow:

      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:fillViewport="true"        
          app:layout_constraintTop_toBottomOf="@id/other_toolbar_xml"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent" >
      
      //....
      
      </ScrollView>