Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamhariomsharma/7ed1d075388c66f578d2f423745b8e49 to your computer and use it in GitHub Desktop.
Save iamhariomsharma/7ed1d075388c66f578d2f423745b8e49 to your computer and use it in GitHub Desktop.
Android alert dialog kotlin Extension
//Utils - Declaration
object PromptUtils {
fun alertDialog(context: Context, @StyleRes style: Int, dialogBuilder: AlertDialog.Builder.() -> Unit): Dialog {
val builder = AlertDialog.Builder(context, style).also {
it.setCancelable(false)
it.dialogBuilder()
}
return builder.create()
}
fun AlertDialog.Builder.negativeButton(text: String = "Dismiss", handleClick: (dialogInterface: DialogInterface) -> Unit = {}) {
this.setPositiveButton(text) { dialogInterface, which -> handleClick(dialogInterface) }
}
fun AlertDialog.Builder.positiveButton(text: String = "Continue", handleClick: (dialogInterface: DialogInterface) -> Unit = {}) {
this.setNegativeButton(text) { dialogInterface, which -> handleClick(dialogInterface) }
}
}
//Usage
PromptUtils.alertDialog(this, R.style.AlertDialog_AppCompat_Light) {
setTitle("Permission")
setMessage("Storeage read and write permission?")
positiveButton(){
//do positive actions
it.dismiss()
}
negativeButton(){
//do negative actions
it.dismiss()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment