Last active
December 27, 2023 04:32
-
-
Save oakkub/887522d1c0dff980a3b63bb5f30a1dbe to your computer and use it in GitHub Desktop.
Kotlin extension functions for creating AlertDialog in a DSL way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} |
👍
show!
you can use inline fun Context.alert()...
instade of Activity or Fragment. Good job!..
If you add How can we use this in Fragment to or activity that would be helpful.
can be used by this way
alert("I AM TITLE", "I AM MESSAGE") {
positiveButton("Ok") {
}
negativeButton("Cancel"){
}
}.show()
Thanks @rakesh-anvd
Thanks, it helped me.
Regards,
Shailendra.
…On Wed, Jul 15, 2020 at 9:42 PM rakeshr-anvd ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
can we used by this way
alert("I AM TITLE", "I AM MESSAGE") {
positiveButton("Ok") {
}
negativeButton("Cancel"){
}
}.show()
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/887522d1c0dff980a3b63bb5f30a1dbe#gistcomment-3378153>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA2UQVPP7LWKFLFU3CG6YI3R3XIP7ANCNFSM4KGVFXOQ>
.
The above util extension can be simplified by using context instead of
Activity.alert(){}
Fragment.alert(){}
with
Context.alert(){}
Yeah got it, thanks
Regards,
Shailendra.
…On Thu, Jul 16, 2020 at 3:31 PM rakeshr-anvd ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@shylendramadda <https://github.com/shylendramadda>
The above util extension can be simplified by using context instead of
Activity.alert(){}
Fragment.alert(){}
with
Context.alert(){}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/887522d1c0dff980a3b63bb5f30a1dbe#gistcomment-3379407>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA2UQVJXTVG6L2AUCBZ34A3R33FV3ANCNFSM4KGVFXOQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank so much