I wanted to create an alert dialog that won't close on hitting its positiveButton
. I modified my code as per many tutorials I found online. But I am not able to get the button, builder.getButton
.
val builder = AlertDialog.Builder(this)
with(builder) {
setTitle("Hello....?")
setCancelable(false)
setPositiveButton("Done", null)
setNegativeButton("Cancel", null)
show()
val positiveButton: Button = builder.getButton(AlertDialog.BUTTON_POSITIVE)
positiveButton.setOnClickListener {
}
You can't get the button on the builder, you need to get it from the dialog (which you create using the builder). Modify your code to:
val builder = AlertDialog.Builder(this)
with(builder) {
setTitle("Hello....?")
setCancelable(false)
setPositiveButton("Done", null)
setNegativeButton("Cancel", null)
val dialog = this.create()
dialog.show()
val positiveButton: Button = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
positiveButton.setOnClickListener {
}
}