anko icon indicating copy to clipboard operation
anko copied to clipboard

Alerts cannot be non cancelable

Open SalomonBrys opened this issue 7 years ago • 7 comments

val pwAlert = alert {
    titleResource = R.string.my_message
    messageResource = R.string.my_message
    okButton {}
    isCancelable = false // This does not exists
}

There is currently no way to create an alert with the alert function and set the dialog to be non cancelable. I am "forced" to revert to java style creation when I need a non cancelable dialog.

BTW, it would also be great if there was a way to access the AlertDialog.Builder.

SalomonBrys avatar Jun 05 '17 13:06 SalomonBrys

A much needed feature

kirtan403 avatar Jul 23 '17 06:07 kirtan403

Before the new feature add to the library, you can try this:

val pwAlert = activity.alert(Appcompat) {
    titleResource = R.string.my_message
    messageResource = R.string.my_message
    okButton {}
}.build()
pwAlert.setCancelable(false)
pwAlert.setCanceledOnTouchOutside(false)
pwAlert.show()

doitlite avatar Aug 15 '17 09:08 doitlite

@doitlite What Appcompat here? please explain more about it

UPDATE: // Appcompat-v7 (only Anko Commons) compile "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version" Use this for Appcompat (alert.build() returns android.support.v7.app AlertDialog)

VijayMakwana avatar Aug 22 '17 13:08 VijayMakwana

In version 0.9 I was able to call cancellable(false). It was removed from builder in 0.10 I wonder if there was a reason for that or was it forgotten during refactoring. It should be easy to restore if there is no reason behind it.

damianpetla avatar Sep 18 '17 12:09 damianpetla

Hi , You can use after ".show()" .setCancelable(false) for example -> alert("Body") { title = "Title" positiveButton("ok") { } }.show().setCancelable(false)

https://gist.github.com/BurakDizlek/4b026b3c8a736987902a7e65f73dc6da

BurakDizlek avatar Oct 05 '17 14:10 BurakDizlek

doitlite is correct:

val pwAlert = activity.alert(Appcompat) {
    titleResource = R.string.my_message
    messageResource = R.string.my_message
    okButton {}
}.build().apply {
    setCancelable(false)
    setCanceledOnTouchOutside(false)
}.show()

Zhuinden avatar Jan 22 '18 11:01 Zhuinden

I want to save dialog after screen rotation. but @doitlite and @Zhuinden don`t helps. I hope the best way is to use ViewModel.

    val errorObservable: LiveData<SignInResult>
    observe(errorObservable, ::handleError)
    private fun handleError(error: Any?) {
        if (error == null) {
            return
        }

        activity.alert(Appcompat) {
            titleResource = R.string.title
            messageResource = R.string.message
            okButton { mSignInViewModel.onErrorClosed() }
        }.build().apply {
            setCancelable(false)
            setCanceledOnTouchOutside(false)
        }.show()
    }

iandreyshev avatar Nov 16 '18 10:11 iandreyshev