Search code examples
androidxmldagger-2android-architecture-navigation

DialogFragment Triggered, but not showing the layout In Navigation Architecture component


In one of my app, I am using Android Navigation Architecture component. Everything was going fine and smooth. But when I wanted to navigate to DialogFrgment, I got stuck. Though DialogFragment triggered, but no layout is seen. Just visible the overlap blur background. Can't figure out the actual problem. Here is the code I use to display dialogFragment.

This is custom layout I wanted to show

terms_and_condition.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:ignore="WebViewLayout">

    <LinearLayout
        android:id="@+id/linearLayout6"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_60sdp"
        android:background="@color/colorAccent"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:weightSum="4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textview.MaterialTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_20sdp"
            android:layout_weight="3"
            android:text="@string/terms_amp_conditions"
            android:textSize="@dimen/_15ssp"
            android:textStyle="bold"
            android:textColor="@color/white" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/close_privacy_policy_dialog_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_close" />

    </LinearLayout>

    <WebView
        android:id="@+id/privacy_policy_web_view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_300sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout6"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the base class for Dialog fragment

BaseDialogFragment.kt

abstract class BaseDialogFragment : DaggerAppCompatDialogFragment() {

    @LayoutRes
    abstract fun getLayoutResource(): Int

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return container?.inflate(getLayoutResource())
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.let {
            it.requestFeature(FEATURE_NO_TITLE)
            it.setBackgroundDrawableResource(R.color.transparent)
        }
        return dialog
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        dialog?.window?.setLayout(
            MATCH_PARENT,
            WRAP_CONTENT
        )

    }

    protected fun disableDismissOnBackPress() {
        dialog?.setOnKeyListener { _, keyCode, event ->
            if (keyCode == KEYCODE_BACK) {
                if (event.action != ACTION_DOWN) true else true
            } else {
                false
            }
        }
    }

}

TermsAndConditionDialogFragment.kt

class TermsAndConditionDialogFragment: BaseDialogFragment() {

    override fun getLayoutResource() = R.layout.dialog_terms_and_condition

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val closeTheDialogImageView = view.findViewById<ImageView>(R.id.close_privacy_policy_dialog_image_view)
        val privacyPolicyWebView = view.findViewById<WebView>(R.id.privacy_policy_web_view)
        privacyPolicyWebView.loadUrl("file:///android_asset/privacy_policy.html")

        closeTheDialogImageView.setOnClickListener { dialog?.dismiss() }
    }

}

navigation_graph.xml

<navigation
    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:id="@+id/navigation_graph"
    app:startDestination="@id/splashFragment">
    <dialog
        android:id="@+id/termsAndConditionDialogFragment"
        android:name="com.aomi.mybase.ui.feature.termAndCondition.TermsAndConditionDialogFragment"
        tools:layout="@layout/dialog_terms_and_condition"/>


</navigation>

SignUpFragment.kt

terms_and_condition_button.setOnClickListener {
    findNavController().navigate(R.id.termsAndConditionDialogFragment)
}

Solution

  • Change your onCreateDialog like below and replace your DaggerAppCompatDialogFragment to DialogFragment

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return this.activity?.let {
          val builder = AlertDialog.Builder(it)
          val inflater = requireActivity().layoutInflater
    
          val layoutId: Int = R.layout.your_layout
    
          val rootView = inflater.inflate(layoutId, null)
          builder.setView(rootView)
    
          val alertDialog = builder.create()
          alertDialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
          alertDialog
        } ?: throw IllegalStateException("Activity cannot be null")
      }