Search code examples
androidkotlincamerapopupalert

Show Dialog before opening the camera


I want to display a message before the camera opens.

Right now the user clicks a button and the message is displayed for 1 second and then the camera opens immediatly. Once i close the camera the dialog is still visible.

I want that the camera only opens after you click okay on the alert message and once you close the camera the message is not shown anymore.

//Button Picture
    cameraBtn.setOnClickListener {
        showDialog()
        pb.visibility = View.VISIBLE
        checkPermission(Manifest.permission.CAMERA,
            CAMERA_PERMISSION_CODE)
        startActivityForResult(receiptsViewModel.cameraIntent(requireActivity()),REQUEST_CODE_KAMERA)
    }

   fun showDialog() {
    val dialogBuilder = AlertDialog.Builder(context)
    dialogBuilder.setMessage("The message here")
    dialogBuilder.setPositiveButton("Done",
        DialogInterface.OnClickListener { dialog, whichButton -> })
    val b = dialogBuilder.create()
    b.show()
}

Solution

  • You have to implement what you have on your cameraBtn into the alert

    val dialogBuilder = AlertDialog.Builder(context)
        dialogBuilder.setMessage("The message here")
        
        dialogBuilder.setPositiveButton("Done"){dialogInterface, which ->  
            pb.visibility = View.VISIBLE
            checkPermission(Manifest.permission.CAMERA,CAMERA_PERMISSION_CODE)
            startActivityForResult(receiptsViewModel.cameraIntent(requireActivity()),REQUEST_CODE_KAMERA)
        }  
    
    val b = dialogBuilder.create()
        b.show()