1

I want to create a dialog with round corners using ConstraintLayout, DialogFragment and a custom background.

The dialog must have a scroll area at the top and a custom button at the bottom. The dialog should resize in height based on the size of the scroll view. I tried on Android 6 and Android 9 but the issue with the above combination is present on both platforms.

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="wrap_content"
    android:fitsSystemWindows="true"
    android:paddingStart="12dp"
    android:paddingTop="8dp"
    android:paddingEnd="12dp"
    android:paddingBottom="8dp"
    app:layout_constrainedHeight="true">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:contentDescription="@null"
        android:scaleType="fitXY"
        android:src="@drawable/animated_vector_cross"
        app:layout_constraintBottom_toTopOf="@+id/tv_title"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        android:text="@string/info"
        android:textColor="@color/sweet_dialog_bg_color_dark"
        android:textSize="@dimen/fs_XXLarge_Land"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/scrollView5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/iv_icon" />

    <TextView
        android:id="@+id/bt_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/dialog_accept_bt_ripple"
        android:clickable="true"
        android:focusable="true"
        android:paddingStart="8dp"
        android:paddingTop="0dp"
        android:paddingEnd="8dp"
        android:paddingBottom="0dp"
        android:text="@string/dialog_ok"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/guideline3"
        app:layout_constraintTop_toBottomOf="@+id/scrollView5"
        tools:ignore="ContentDescription" />

    <ScrollView
        android:id="@+id/scrollView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:fillViewport="false"
        android:orientation="vertical"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@+id/bt_ok"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title">

        <TextView
            android:id="@+id/tv_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum"
            android:textSize="@dimen/fs_XLarge_Land" />
    </ScrollView>

    <android.support.constraint.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" /> </android.support.constraint.ConstraintLayout>

The problem is that if the text that I put inside the scroll view is big the bottom button will be pushed outside of the dialog.

With little text the dialog shrinks but the bottom button is correctly displayed.

With big text the button is pushed outside.

What settings do I miss?

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
Claudiu
  • 21
  • 3

1 Answers1

1

The issue was solved by wrapping the ConstraintLayout with a LinearLayout and using wrap_content parameters.

Claudiu
  • 21
  • 3