Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin / Anko prevent button from closing Alert Dialog

When using positiveButton and negativeButton in Anko's alert builder, it seems both of them result in closing the dialog, even if dismiss() is not called. Is there any way to keep the dialog open after clicking a button (if there are types other than positiveButton/negativeButton, that's fine too)?

alert {
    title = "Add Board"
    customView {
        ....
    }
    positiveButton("OK") { doSomeFunction() }
    negativeButton("Close"){}
}.show()
like image 399
Parker Avatar asked Jan 18 '26 10:01

Parker


1 Answers

For anyone that may have this issue in the future, this is how you can accomplish this in Kotlin

val myAlert = alert {
    title = "Add Board"
    customView {
        ....
    }
    positiveButton("OK") { /*Keep blank, we'll override it later*/}
    negativeButton("Close"){}
    }.show()

//You can use BUTTON_NEGATIVE and BUTTON_NEUTRAL for other buttons
(myAlert as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE)
    .setOnclickListener{
        doSomeFunction()
    }
like image 177
Parker Avatar answered Jan 21 '26 01:01

Parker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!