Another way to do it is to use AlertDialog
from the AndroidX AppCompat library:
AlertDialog signInDialog = new AlertDialog.Builder(this)
.setMessage("Are you sure you want to log out?")
.setPositiveButton("OK", (dialogInterface, i) -> {
// TODO: Add your code here
})
.setNegativeButton("Cancel", (dialogInterface, i) -> {
// TODO: Add your code here
})
signInDialog.show();
Note: If you're using the Material Components for Android library and/or you're using the new Theme.MaterialComponents.*
themes, you should instead use the MaterialAlertDialogBuilder
class, which should be used in place of the AppCompat AlertDialog
class as described below:
Material maintains usage of the framework AlertDialog
, but provides a new builder, MaterialAlertDialogBuilder
, which configures the instantiated AlertDialog with Material specs and theming.
Here's an example (in Kotlin):
MaterialAlertDialogBuilder(this).apply {
setMessage("Are you sure you want to log out?")
setPositiveButton("OK") {
TODO("Unimplemented functionality")
}
setNegativeButton("Cancel") {
TODO("Unimplemented functionality")
}
}.show()