Search code examples
kotlinandroid-alertdialog

Kotlin how to disable a btn depending on condition


I have seen a code that do the following:

button.isEnabled = false
button.isClickable = false

I do not know if this was an older way of doing it. I am just not sure how it will be implemented into my code:

 lateinit var dialog:AlertDialog

        // Initialize an array of colors
        var checked = 0
        val items = arrayOf("CHECKED", "UNCHECKED")

        val builder = AlertDialog.Builder(context)
        builder.setTitle(R.string.dialogTitleDel)

        builder.setSingleChoiceItems(items,-1) { _, which ->
            checked = which + 1
        }

        builder.setPositiveButton("Yes"){dialogInterface, which ->
            if( checked > 0){

            modal.tvdone = 1
            Log.e("Clicked", "Successful delivery")

            notifyDataSetChanged()
        
            }
           // else{PositiveButton.setEnabled(false)}
        }
        dialog = builder.create()
        dialog.setCancelable(false)
        dialog.show()

What will be the correct way of disabling the positive button, until a condition is met?


Solution

  • It's not clear what you are trying to achieve as per your commented line your are trying to disable the positive button on positive button click

    Regardless, you need to get the dialog's positive button and then enable/disable it

    dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false