Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add a dismiss on a listener argument in a AlertDialog

I’m doing a method to create a custom AlertDialog in which I can call this method passing text, and listener to open a dialog in everywhere.

My problem is that when I’m use it, I need to call a dismiss in every button of the custom view, but when I call this method the system no need to know anything about the dialog.

So, I need to add a "dismiss" on the passed listener or something like that.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private fun showCustomAlertDialog(context: Context, title: String?, message: String?, positiveButtonClickListener: View.OnClickListener?, negativeButtonClickListener: View.OnClickListener?, neutralButtonClickListener: View.OnClickListener?, positiveText: String?, negativeText: String?, neutralText: String?){

        val layoutInflater: LayoutInflater = layoutInflater
        val alertLayout: View = layoutInflater.inflate(R.layout.layout_dialog, null)
        val tvDialogTitle: TextView = alertLayout.findViewById(R.id.tv_dialog_title)
        val tvDialogDescription: TextView = alertLayout.findViewById(R.id.tv_dialog_description)
        val btnAccept: Button = alertLayout.findViewById(R.id.btn_dialog_accept)
        val btnCancel: Button = alertLayout.findViewById(R.id.btn_dialog_cancel)
        val btnNeutral: Button = alertLayout.findViewById(R.id.btn_dialog_neutral)

        val alertDialog = AlertDialog.Builder(context)

        alertDialog.setCancelable(false)
        alertDialog.setView(alertLayout)
        val dialog = alertDialog.create()

        if (title != null) {
            if(title.isNotEmpty()){
                tvDialogTitle.text = title
            }
        }else{
            tvDialogTitle.text = ""
        }

        if (message != null) {
            if(message.isNotEmpty()){
                tvDialogDescription.text = message
            }
        }else{
            tvDialogDescription.text = ""
        }

        if(neutralButtonClickListener != null && (positiveButtonClickListener == null && negativeButtonClickListener == null)){
            btnAccept.visibility = View.GONE
            btnCancel.visibility = View.GONE
            btnNeutral.visibility = View.VISIBLE

            btnNeutral.setOnClickListener(neutralButtonClickListener)
            if (neutralText != null) {
                if(neutralText.isNotEmpty()){
                    btnNeutral.text = neutralText
                }
            }
        }

        if(neutralButtonClickListener == null && (positiveButtonClickListener != null && negativeButtonClickListener != null)) {
            btnAccept.visibility = View.VISIBLE
            btnCancel.visibility = View.VISIBLE
            btnNeutral.visibility = View.GONE

            btnAccept.setOnClickListener(positiveButtonClickListener)
            btnCancel.setOnClickListener(negativeButtonClickListener)

            if (positiveText != null) {
                if(positiveText.isNotEmpty()){
                    btnAccept.text = positiveText
                }
            }

            if (negativeText != null) {
                if(negativeText.isNotEmpty()){
                    btnCancel.text = negativeText
                }
            }

        }

        dialog.show()

    }

At this moment I don’t know how to add a dismiss after doing the listener that I passed. For example:

btnAccept.setOnClickListener(positiveButtonClickListener)

Any advice?

Thank you

>Solution :

fun showCustomDialog(
            context: Context,
            title: String?,
            onPositiveButtonClicked: (() -> Unit)? = null,
            onNegativeButtonClicked: (() -> Unit)? = null
        ) {
            val binding =
                LayoutCustomDialogBinding.inflate(LayoutInflater.from(context))

            val alertDialog = MaterialAlertDialogBuilder(context)
                .setView(binding.root)
                .create()

            binding.positiveButton.setOnClickListener {
                 alertDialog.dismiss()
                 onPositiveButtonClicked?.invoke()
            }

            binding.negativeButton.setOnClickListener {
                 alertDialog.dismiss()
                 onNegativeButtonClicked?.invoke()
            }
            
            alertDialog.show()
         }

Usage:

showCustomDialog(
    context,
    title = "Title",
    onPositiveButtonClicked = {
        //do your action
    }, 
    onNegativeButtonClicked = {
        //do your action
    }
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading