Search code examples
androidkotlindialogtouchandroid-alertdialog

BottomSheetDialog setCanceledOnTouchOutside Kotlin


I created a bottom sheet dialog and am implementing cancel function.

It has to be not dismissed when outside of the dialog is touched, but the bottom navigation should catch the touch. After a long trial, I realized I cannot use "setCanceledOnTouchOutside" on Kotlin. "isCancelable=false" is working but cannot use backbutton on the bottom navigation. How should I do if I want to make the bottom navigation touchable only?

Any help will be greatly appreciated

My code is here

class BiometricChangeDetectDialog: BottomSheetDialogFragment() {
private var _binding: DialogBiometricChangeDetectBinding? = null
private val binding get() = _binding!!
private lateinit var mContext: MainActivity

override fun onAttach(context: Context) {
    super.onAttach(context)
    mContext = context as MainActivity
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    
    AlertDialog.Builder(requireContext()).apply {
        isCancelable = false
        //change here to setCanceledOnTouchOutside
    }

}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
    _binding = DialogBiometricChangeDetectBinding.inflate(inflater, container, false)
    val view = binding.root

    binding.btnEmail.setOnClickListener {
        dismiss()
        findNavController().navigate(R.id.action_biometricChangeDetectDialog_to_biometricChangeEmailDialog)
    }

    binding.btnSms.setOnClickListener {
        dismiss()
        findNavController().navigate(R.id.action_biometricChangeDetectDialog_to_biometricChangeEmailDialog)
    }
    return view
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}

}


Solution

  • add this line in onCreateView

    dialog?.setCanceledOnTouchOutside(false)