Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.88k stars 1.29k forks source link

Alerts cannot be non cancelable #397

Open SalomonBrys opened 7 years ago

SalomonBrys commented 7 years ago
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.

kirtan403 commented 7 years ago

A much needed feature

doitlite commented 7 years ago

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()
VijayMakwana commented 7 years ago

@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)

damianpetla commented 7 years ago

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.

BurakDizlek commented 7 years ago

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

Zhuinden commented 6 years ago

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()
iandreyshev commented 6 years ago

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()
    }