Kotlin / anko

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

How to get instance of AlertDialog #419

Open Rahulkr2 opened 7 years ago

Rahulkr2 commented 7 years ago

How to get instance of AlertDialog from anko alert() Like `val builder = AlertDialog.Builder(context) .setTitle("Enter Consis") .setView(view) .setNegativeButton("Cancel") { dialog: DialogInterface, which: Int ->
dialog.dismiss() } .setPositiveButton("Search") { dialog: DialogInterface, which: Int -> {} }

        alertDialog = builder.create()
        alertDialog!!.show()`

How can i achieve above using anko alert() function

ParkerK commented 7 years ago

Check the wiki

Rahulkr2 commented 7 years ago

val dd: AlertDialog = alert { }

Above code giving below error.

Error:(621, 35) Type mismatch: inferred type is AlertBuilder but AlertDialog was expected

How to do it correctly.

nvlled commented 7 years ago

What about

val dialog: DialogInterface = alert { }.show()
if (dialog is AlertDialog) {
    // dialog is now smart-casted to AlertDialog
    dialog.dismiss()
}