'ScrollView does not show content when its constrained by "Match-constrains" inside a layout

I have a ScrollView inside my Constrained layout which contains another constrained layout, the view does not show its content if I set its height to "Match-Constrained" i.e.( 0dp). if I set it to something hardcoded like 400dp then it works.

I tried to replicate the same in a test application and that worked fine but does not work in my application.

the scroll view is named "questionview" is included in the end in the main layout.

Here's the main layout:

<?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"
    android:id="@+id/fragmentExamLayout"
    android:padding="15dp"
    tools:context=".ui.examination.ExamSectionFragment">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/questionTab"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/exitTestButton"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabIndicatorGravity="top"
        app:tabIndicatorHeight="2dp"
        app:tabMinWidth= "@dimen/tab_min_width"
        app:tabMode="scrollable"
        app:tabRippleColor="@color/colorAccent"
        app:tabSelectedTextColor="@color/colorPrimary" />

    <TextView
        android:id="@+id/timerText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/timer"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/question_text_size"
        app:autoSizeTextType="uniform"
        app:layout_constraintEnd_toStartOf="@+id/exitTestButton"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/exitTestButton"
        style="@android:style/Widget.DeviceDefault.Light.Button.Borderless.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="@string/submit_test"
        android:textColor="#CF0A0A"
        app:layout_constraintBottom_toBottomOf="@+id/timerText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/timerText"
        app:layout_constraintTop_toTopOf="@+id/timerText" />

    <TextView
        android:id="@+id/questionNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/QuestionNum"
        android:textSize="@dimen/heading_text_size"
        android:textStyle="bold"
        app:layout_constrainedHeight="false"
        app:layout_constraintEnd_toStartOf="@+id/questionMMarks"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toEndOf="@+id/include3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/questionTab" />

    <TextView
        android:id="@+id/questionMMarks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:text="@string/m_m"
        android:textSize="@dimen/heading_text_size"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/questionNumber"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/questionNumber" />

    <include
        android:id="@+id/include3"
        layout="@layout/questionview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/questionNumber" />

</androidx.constraintlayout.widget.ConstraintLayout>

the ScrollView in added via "include" tag

here's the code for scroll view :

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:id="@+id/scrollView3"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@android:drawable/divider_horizontal_dim_dark"
        android:padding="16dp"
        android:showDividers="beginning">

        <TextView
            android:id="@+id/questionText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="@dimen/question_text_size"
            app:autoSizeTextType="uniform"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.514"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/questionText">

            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text=""
                android:textSize="@dimen/radio_text_size"/>

            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text=""
                android:textSize="@dimen/radio_text_size"/>

            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text=""
                android:textSize="@dimen/radio_text_size"/>

            <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/text_margin"
                android:text=""
                android:textSize="@dimen/radio_text_size"/>
        </RadioGroup>

        <Button
            android:id="@+id/saveAndNextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/buttonshape"
            android:paddingHorizontal="@dimen/button_padding_horizontal"
            android:text="@string/save_and_next"
            android:textColor="#FFFFFF"
            android:textSize="@dimen/buttton_text_size"
            app:layout_constraintBottom_toBottomOf="@+id/markReviewButton"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/markReviewButton"
            app:layout_constraintTop_toTopOf="@+id/markReviewButton"
            app:layout_constraintVertical_bias="0.396" />

        <Button
            android:id="@+id/markReviewButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="36dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="24dp"
            android:background="@drawable/buttonshape"
            android:paddingHorizontal="@dimen/button_padding_horizontal"
            android:text="@string/mark_for_review"
            android:textColor="#FFFFFF"
            android:textSize="@dimen/buttton_text_size"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/saveAndNextButton"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintHorizontal_chainStyle="spread_inside"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/radioGroup"
            app:layout_constraintVertical_bias="0.0" />


    </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

Its should look like this:

Layout image

if I change layout height in include to fixed height like 400dp as shown:

 <include
        android:id="@+id/include3"
        layout="@layout/questionview"
        android:layout_width="0dp"
        

android:layout_height="400dp"

        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/questionNumber" />

the ScrollView works just fine then.



Solution 1:[1]

Turns out, I had set component height to wrap content in outermost parent.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shresthdeep Gupta