Skip to content

Instantly share code, notes, and snippets.

@Williamrai
Last active October 18, 2024 15:26
Show Gist options
  • Save Williamrai/68574cf5959bd0ab537aa5f0f57bda78 to your computer and use it in GitHub Desktop.
Save Williamrai/68574cf5959bd0ab537aa5f0f57bda78 to your computer and use it in GitHub Desktop.
// we would be writing this reptitive code multiple times for similar dialogs
MaterialAlertDialogBuilder(context)
.setTitle(R.string.confirmation_dialog_title)
.setMessage(R.string.confirmation_dialog_message)
.setIcon(R.drawable.ic_feedback)
.setPositiveButton("Okay") { _, _ ->
deleteItem()
}
.setNegativeButton("Cancel", null)
.setCancelable(false)
.show()
// and for the dialog with delay we would also have to do this or create this in a function with delay logic, but if we
// add this in function then we would have another function to maintain which we may forget later
MaterialAlertDialogBuilder(context)
.setTitle(R.string.confirmation_survey_dialog_title)
.setMessage(R.string.confirmation_survey_dialog_message)
.setIcon(R.drawable.ic_feedback)
.setPositiveButton("Okay") { _, _ ->
deleteItem()
}
.setNegativeButton("Cancel", null)
.setCancelable(false)
.show()
// much cleaner and also provides simple manageable functionality like delay
showSimpleAlertDialog {
title = R.string.confirmation_dialog_title
message = R.string.confirmation_dialog_message
icon = R.drawable.feedback
isCancellable = false
delayMillis = 1000
positiveButton("Okay") {
deleteItem()
}
negativeButton("Cancel") {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment