-
Notifications
You must be signed in to change notification settings - Fork 1
SettingsAlertDialog
Leonardo D. Palma edited this page Nov 14, 2024
·
4 revisions
Create and display a dialog box that directs us to the application settings.
In code:
private val launchSettings = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
Toast.makeText(this, "Settings activity closed", Toast.LENGTH_SHORT).show()
}
...
SettingsAlertDialog.Builder(this@MainActivity)
.setLaunch(launchSettings)
.setCancelable(false)
.setPositiveButton(null, object : MaterialDialogInterface.OnClickListener {
override fun onClick(dialog: MaterialDialogInterface?, whichButton: AlertDialog.UI?) {
dialog?.dismiss()
}
})
.setNegativeButton(null, object : MaterialDialogInterface.OnClickListener {
override fun onClick(dialog: MaterialDialogInterface?, whichButton: AlertDialog.UI?) {
dialog?.dismiss()
}
})
.create()
.show()SettingsAlertDialog offers several attributes for a deeper view configuration, the following table shows all these options and their default value.
| Name | Description | Values | Default | Related method(s) |
|---|---|---|---|---|
| Builder | Creates a builder for an alert dialog that uses the default alert dialog theme | Context |
N/A | Builder() |
| Icon | Set the drawable resource to be used in the icon | Int |
R.mipmap.ic_launcher_round |
setIcon() |
| Title | Set the title displayed |
String, Int
|
R.string.text_value_title_settings_dialog |
setTitle() |
| Message | Sets the message to display |
String, Int
|
R.string.text_value_rationale_ask_again |
setMessage() |
| Launch | Set launcher for intent | ActivityResultLauncher<Intent> |
N/A | setLaunch() |
| OpenOnNewTask | Sets whether the intent will open in a new task | Boolean |
false |
setOpenOnNewTask() |
| Cancelable | Sets whether the dialog is cancelable or not | Boolean |
true |
setCancelable() |
| PositiveButton | Set a listener to be invoked when the positive button of the dialog is pressed |
null, String or Int
|
N/A | setPositiveButton() |
| NeutralButton | Set a listener to be invoked when the neutral button of the dialog is pressed |
null, String or Int
|
N/A | setNeutralButton() |
| NegativeButton | Set a listener to be invoked when the negative button of the dialog is pressed |
null, String or Int
|
N/A | setNegativeButton() |
| create | Creates an SettingsAlertDialog with the arguments supplied to this builder | - | - | create() |
| show | Start the dialog and display it on screen | - | - | show() |
If you want to add more functionality to dialog events, you can use the following methods to capture the actions.
- When the
dismissoption is called from the dialog box, you can use thesetOnDismissListenermethod to perform additional actions. - When the
canceloption is called from the dialog box, you can use thesetOnCancelListenermethod to perform additional actions. - When the
showoption is called from the dialog box, you can use thesetOnShowListenermethod to perform additional actions.