Last active
August 6, 2018 11:51
-
-
Save vad-zuev/c8e0f176af24837ba9c3fd420d915d14 to your computer and use it in GitHub Desktop.
Shows an AlertDialog that doesn't break an active immersive mode. Credits go to Beaver6813 for the original idea (https://stackoverflow.com/questions/22794049/how-do-i-maintain-the-immersive-mode-in-dialogs/23207365)
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
/** | |
* Creates and shows an [AlertDialog] while maintaining the immersive mode. | |
* @param activity the `Activity` which will host the dialog | |
* @param title dialog's title | |
* @param message dialog's message | |
* @param cancelable whether the dialog should be cancelable | |
* @param yesButton the text for the positive button (no positive button will be added if `null` is passed) | |
* @param noButton the text for the negative button (no negative button will be added if `null` is passed) | |
* @param yesCallback callback to invoke when the user presses the positive button | |
* @param noCallback callback to invoke when the user presses the negative button | |
* @return the dialog | |
*/ | |
fun showAlertDialog(activity: Activity, title: String, message: String, cancelable: Boolean = true, | |
yesButton: String? = null, noButton: String? = null, | |
yesCallback: (() -> Unit)? = null, noCallback: (() -> Unit)? = null): Dialog { | |
val builder = AlertDialog.Builder(activity) | |
.setTitle(title) | |
.setMessage(message) | |
.setCancelable(cancelable) | |
yesButton?.let { builder.setPositiveButton(yesButton) { _, _ -> yesCallback?.invoke() } } | |
noButton?.let { builder.setNegativeButton(noButton) { _, _ -> noCallback?.invoke() } } | |
return builder.create().apply { | |
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) | |
show() | |
window.decorView.systemUiVisibility = activity.window.decorView.systemUiVisibility | |
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment