Skip to content

Instantly share code, notes, and snippets.

@ch8n
Last active October 3, 2023 12:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ch8n/319a60251885ad538f149e9a1037f7b9 to your computer and use it in GitHub Desktop.
Save ch8n/319a60251885ad538f149e9a1037f7b9 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()
}
}
@EbenezerGH
Copy link

This is real nice ๐Ÿ‘

@RahulSDeshpande
Copy link

RahulSDeshpande commented Dec 21, 2020

Great!

Here is my version for MaterialAlertDialog (my gist):

// Kotlin extension
fun Context.alert(
    @StyleRes style: Int = 0,
    dialogBuilder: MaterialAlertDialogBuilder.() -> Unit
) {
    MaterialAlertDialogBuilder(this, style)
        .apply {
            setCancelable(false)
            dialogBuilder()
            create()
            show()
        }
}

fun MaterialAlertDialogBuilder.negativeButton(
    text: String = "No",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setNegativeButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

fun MaterialAlertDialogBuilder.positiveButton(
    text: String = "Yes",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setPositiveButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

fun MaterialAlertDialogBuilder.neutralButton(
    text: String = "OK",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setNeutralButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

// Usage
alert {
    setTitle("Confirm")
    setMessage("Are you sure you want to delete this item?")
    positiveButton { // Do something }
    negativeButton { // Do something }
}

@ch8n
Copy link
Author

ch8n commented Dec 24, 2020

Great!

Here is my version for MaterialAlertDialog (my gist):

// Kotlin extension
fun Context.alert(
    @StyleRes style: Int = 0,
    dialogBuilder: MaterialAlertDialogBuilder.() -> Unit
) {
    MaterialAlertDialogBuilder(this, style)
        .apply {
            setCancelable(false)
            dialogBuilder()
            create()
            show()
        }
}

fun MaterialAlertDialogBuilder.negativeButton(
    text: String = "No",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setNegativeButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

fun MaterialAlertDialogBuilder.positiveButton(
    text: String = "Yes",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setPositiveButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

fun MaterialAlertDialogBuilder.neutralButton(
    text: String = "OK",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setNeutralButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

// Usage
alert {
    setTitle("Confirm")
    setMessage("Are you sure you want to delete this item?")
    positiveButton { // Do something }
    negativeButton { // Do something }
}

@RahulSDeshpande

Hi would you like to contribute same in my awsome list https://github.com/ch8n/awesome-kotlin-extensions

@RahulSDeshpande
Copy link

RahulSDeshpande commented Jan 26, 2021

@ch8n

Hey sure!
I would love to!

@davidkocnar
Copy link

@RahulSDeshpande ๐Ÿ‘Œ nice

@RahulSDeshpande
Copy link

@davidkocnar

Thank you ๐Ÿ™‚๐Ÿ™‚โœŒ๐ŸผโœŒ๐Ÿผ

@emresahin10
Copy link

Great!

Here is my version for MaterialAlertDialog (my gist):

// Kotlin extension
fun Context.alert(
    @StyleRes style: Int = 0,
    dialogBuilder: MaterialAlertDialogBuilder.() -> Unit
) {
    MaterialAlertDialogBuilder(this, style)
        .apply {
            setCancelable(false)
            dialogBuilder()
            create()
            show()
        }
}

fun MaterialAlertDialogBuilder.negativeButton(
    text: String = "No",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setNegativeButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

fun MaterialAlertDialogBuilder.positiveButton(
    text: String = "Yes",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setPositiveButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

fun MaterialAlertDialogBuilder.neutralButton(
    text: String = "OK",
    handleClick: (dialogInterface: DialogInterface) -> Unit = { it.dismiss() }
) {
    this.setNeutralButton(text) { dialogInterface, _ -> handleClick(dialogInterface) }
}

// Usage
alert {
    setTitle("Confirm")
    setMessage("Are you sure you want to delete this item?")
    positiveButton { // Do something }
    negativeButton { // Do something }
}

thank you sir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment