Skip to content

Instantly share code, notes, and snippets.

@oakkub
Last active December 27, 2023 04:32
Show Gist options
  • Save oakkub/887522d1c0dff980a3b63bb5f30a1dbe to your computer and use it in GitHub Desktop.
Save oakkub/887522d1c0dff980a3b63bb5f30a1dbe to your computer and use it in GitHub Desktop.
Kotlin extension functions for creating AlertDialog in a DSL way
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.support.annotation.StringRes
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.eggdigital.trueyouedc.R
/**
* Created by oakkub on 10/5/2017 AD.
*/
inline fun Activity.alert(title: CharSequence? = null, message: CharSequence? = null, func: AlertDialogHelper.() -> Unit): AlertDialog {
return AlertDialogHelper(this, title, message).apply {
func()
}.create()
}
inline fun Activity.alert(titleResource: Int = 0, messageResource: Int = 0, func: AlertDialogHelper.() -> Unit): AlertDialog {
val title = if (titleResource == 0) null else getString(titleResource)
val message = if (messageResource == 0) null else getString(messageResource)
return AlertDialogHelper(this, title, message).apply {
func()
}.create()
}
inline fun Fragment.alert(title: CharSequence? = null, message: CharSequence? = null, func: AlertDialogHelper.() -> Unit): AlertDialog {
return AlertDialogHelper(context, title, message).apply {
func()
}.create()
}
inline fun Fragment.alert(titleResource: Int = 0, messageResource: Int = 0, func: AlertDialogHelper.() -> Unit): AlertDialog {
val title = if (titleResource == 0) null else getString(titleResource)
val message = if (messageResource == 0) null else getString(messageResource)
return AlertDialogHelper(context, title, message).apply {
func()
}.create()
}
@SuppressLint("InflateParams")
class AlertDialogHelper(context: Context, title: CharSequence?, message: CharSequence?) {
private val dialogView: View by lazyFast {
LayoutInflater.from(context).inflate(R.layout.dialog_info, null)
}
private val builder: AlertDialog.Builder = AlertDialog.Builder(context)
.setView(dialogView)
private val title: TextView by lazyFast {
dialogView.findViewById<TextView>(R.id.dialogInfoTitleTextView)
}
private val message: TextView by lazyFast {
dialogView.findViewById<TextView>(R.id.dialogInfoMessageTextView)
}
private val positiveButton: Button by lazyFast {
dialogView.findViewById<Button>(R.id.dialogInfoPositiveButton)
}
private val negativeButton: Button by lazyFast {
dialogView.findViewById<Button>(R.id.dialogInfoNegativeButton)
}
private var dialog: AlertDialog? = null
var cancelable: Boolean = true
init {
this.title.text = title
this.message.text = message
}
fun positiveButton(@StringRes textResource: Int, func: (() -> Unit)? = null) {
with(positiveButton) {
text = builder.context.getString(textResource)
setClickListenerToDialogButton(func)
}
}
fun positiveButton(text: CharSequence, func: (() -> Unit)? = null) {
with(positiveButton) {
this.text = text
setClickListenerToDialogButton(func)
}
}
fun negativeButton(@StringRes textResource: Int, func: (() -> Unit)? = null) {
with(negativeButton) {
text = builder.context.getString(textResource)
setClickListenerToDialogButton(func)
}
}
fun negativeButton(text: CharSequence, func: (() -> Unit)? = null) {
with(negativeButton) {
this.text = text
setClickListenerToDialogButton(func)
}
}
fun onCancel(func: () -> Unit) {
builder.setOnCancelListener { func() }
}
fun create(): AlertDialog {
title.goneIfTextEmpty()
message.goneIfTextEmpty()
positiveButton.goneIfTextEmpty()
negativeButton.goneIfTextEmpty()
dialog = builder
.setCancelable(cancelable)
.create()
return dialog!!
}
private fun TextView.goneIfTextEmpty() {
visibility = if (text.isNullOrEmpty()) {
View.GONE
} else {
View.VISIBLE
}
}
private fun Button.setClickListenerToDialogButton(func: (() -> Unit)?) {
setOnClickListener {
func?.invoke()
dialog?.dismiss()
}
}
}
@ozrozcn
Copy link

ozrozcn commented Apr 18, 2020

you can use inline fun Context.alert()... instade of Activity or Fragment. Good job!..

@shylendramadda
Copy link

If you add How can we use this in Fragment to or activity that would be helpful.

@rakeshr-anvd
Copy link

rakeshr-anvd commented Jul 15, 2020

can be used by this way

alert("I AM TITLE", "I AM MESSAGE") {
      positiveButton("Ok") {
      }
      negativeButton("Cancel"){
      }
  }.show()

@shylendramadda
Copy link

Thanks @rakesh-anvd

@shylendramadda
Copy link

shylendramadda commented Jul 16, 2020 via email

@rakeshr-anvd
Copy link

@shylendramadda

The above util extension can be simplified by using context instead of

Activity.alert(){}
Fragment.alert(){}

with

Context.alert(){}

@shylendramadda
Copy link

shylendramadda commented Jul 16, 2020 via email

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