Search code examples
androidandroid-dialogfragmentandroid-jetpack-navigation

Displaying DialogFragment with NavHostFragment displays empty


Im trying to display a DialogFragment which after showing up on the screen should display a start destination from the nav_graph specified in the NavHostFragment. Here's DialogFragment layout:

<?xml version="1.0" encoding="utf-8"?>
<layout>

<!--    <androidx.constraintlayout.widget.ConstraintLayout -->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="800dp">-->

<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:visibility="visible">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="8dp"
            android:orientation="vertical">

            <fragment
                android:id="@+id/nav_host_home_bottom_sheet"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@+id/dismiss_registration_button"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/membership_navigation" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/dismiss_registration_button"
                style="@style/Widget.MaterialComponents.Button.TextButton"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/open_sans_regular"
                android:text="@string/all_dismiss"
                android:textSize="14sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

<!--    </androidx.constraintlayout.widget.ConstraintLayout>-->

What happens is only Dismiss button displays in the DialogFragment. When I uncomment ConstraintLayout with layout_height fixed to 800dp then startDestination from NavHostFragment loads ok. The problem is that I don't want to hadrcode the layout_height - how can I display it with either CoordinatorLayou or ConstraintLayout without hardcoding the layout_height?


Solution

  • To make it work I simply changed DialogFragment layout to

    <?xml version="1.0" encoding="utf-8"?>
    <layout>
    <androidx.coordinatorlayout.widget.CoordinatorLayout 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">
    
        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/membership_navigation" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    and then handle scroll view and others in fragments where it's needed.