I want to show a dialog with a title (header), body (content) and accept button (footer). I want header and footer to be visible even if content is longer than the screen height. If this is the case, I want body to be scrollable so that it can shrink. Here is what I tried, scrollView gets all the dialog space, no header and no footer is shown:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:minWidth="300dp">
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:text="Title"
app:layout_constraintBottom_toTopOf="@+id/bodyContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/bodyContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/buttonContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="1500dp"
android:text="Long text" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/buttonContainer"
android:layout_width="0dp"
android:layout_height="30dp"
android:background="@android:color/darker_gray"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bodyContainer" />
</androidx.constraintlayout.widget.ConstraintLayout>
When your ScrollView's
height is set to wrap_content
the constraints will not limit the dimension if it gets too big to satisfy them. To fix this and enforce the top and bottom constraints, set app:layout_constrainedHeight="true"
for your ScrollView
.