Home > other >  ScrollView containing a TextView disappears in ConstraintLayout when height is set to 0dp
ScrollView containing a TextView disappears in ConstraintLayout when height is set to 0dp

Time:09-22

I am trying to create a custom alertDialog with a ConstraintLayout, containing a title, scrollview with a textview, and a button at the bottom.

I want the scrollview to grow/shrink dynamically and fill the available space between the title and the button, which works in the preview, but when running the actual app, the scrollview and textview seem to actually shrink to 0dp and disappear (as I understand it, 0dp with fillViewport should grow to fit the available space according to the constraints.

Preview

Actual app

layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white">
    <TextView
        android:id="@ id/tvTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="A title"
        android:textSize="20dp"
        android:gravity="start"
        android:textColor="#000000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <ScrollView
        android:id="@ id/svBody"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintTop_toBottomOf="@id/tvTitle"
        app:layout_constraintBottom_toTopOf="@id/btnClose"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        >
        <TextView
            android:id="@ id/tvBody"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="AA text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long.A text that is really long. text that is really long."
            android:textSize="16dp"
            android:textColor="#000000"
            />
    </ScrollView>
    <Button
        android:id="@ id/btnClose"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="close"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="5dp"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

Could this be caused by some conflict with the constraints of the view that is later responsible for rendering this custom alertDialog? Or am I missing something else? I can of course solve it by setting a fixed size on the scrollView, but then it looks small on bigger screens so I'm hoping for a dynamic solution.

Any help is greatly appreciated!

CodePudding user response:

Probably your dialog has no parent. Therefore the match_parent of the ConstraintLayout does not work. So the dialog is limited only to the elements that have wrap_content set. If you display the whole thing in a normal fragment that has an activity, it works. Therefore I would try to use a DialogFragment. There it should work without problems. Besides, it looks even nicer.

Documentation for this: https://developer.android.com/guide/fragments/dialogs

I hope I could help you

CodePudding user response:

I think this should solve your issue.

https://stackoverflow.com/a/3256305/13303290

  • Related