androidandroid-fragmentsandroid-dialog

Add Fragment to custom dialog layout


I'm not sure if this is possible but I have a custom dialog and I'd like to add a Fragment to it. This is all I have so far:

Layout:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_view"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Code:

View layout = getLayoutInflater().inflate(R.layout.custom_layout, null);

Dialog dialog = new Dialog(mActivity);
dialog.setContentView(layout);
dialog.show();

getSupportFragmentManager().beginTransaction()
        .add(R.id.fragment_view, new Fragment(), null)
        .commit();

Solution

  • You should use DialogFragment for that. check out this here