Alert Dialog

Alert Dialog

Alert Dialogs

  • Alert Dialog shows the Alert message and gives the answer in the form of yes or no.
  • Alert Dialog displays the message to warn you and then according to your response, the next step is processed.
  • Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button.

Alert Dialog code has three methods:

  • setTitle() method for displaying the Alert Dialog box Title
  • setMessage() method for displaying the message
  • setIcon() method is used to set the icon on the Alert dialog box.
  • Then we add the two Buttons, setPositiveButton  and setNegativeButton to our Alert Dialog Box
To create an AlertDialog we use the AlertDialog.Builder inner class.
val alertDialog = AlertDialog.Builder(this)

Alert Dialog Methods

Some of the methods that can be used on an AlertDialog.
  • setTitle
  • setMessage
  • setIcon
  • setCustomTitle - Here you can pass a custom view that’ll be put in place of the title part in the alert dialog.
  • setPositiveButton - We pass the string name, as well as Button, clicked callback method here.
  • setView - used to add a custom view inside the alert dialog.
  • setList - used to set an array of strings which would be displayed in the form of a List.
  • setMultiChoiceList - again we can set an array but this time we can select multiple items from the List thanks to CheckBox.
  • setPositiveButtonIcon - set an icon alongside the Button
  • show() - used to display the AlertDialog
  • setDismissListener - Inside this, you can set the logic to be triggered when the alert dialog is dismissed.
  • setShowListener - set the logic to be triggered when the alert dialog is dismissed.
  • setCancelable - requires a boolean value. By default all alert dialogs are cancelable on button click or touch outside. If this method is set to false, you need to explicitly cancel the dialog using dialog.cancel() method.

Alert Dialog Kotlin Code

val builder = AlertDialog.Builder(this) builder.setTitle("Androidly Alert") builder.setMessage("We have a message") builder.setPositiveButton(android.R.string.yes) { dialog, which -> Toast.makeText(applicationContext, android.R.string.yes, Toast.LENGTH_SHORT).show() } builder.setNegativeButton(android.R.string.no) { dialog, which -> Toast.makeText(applicationContext, android.R.string.no, Toast.LENGTH_SHORT).show() } builder.setNeutralButton("Maybe") { dialog, which -> Toast.makeText(applicationContext, "Maybe", Toast.LENGTH_SHORT).show() } builder.show()
  • The builder.show() displays the Alert Dialog on the screen.
  • Inside the setPositiveButton function, we pass the Button text along with a Kotlin function that’s triggered when that button is clicked.
  • The function is a part of the DialogInterface.OnClickListener() interface. The function type is (DialogInterface, Int) -> Unit.
  • DialogInterface is an instance of the Dialog and Int is the id of the button that is clicked. In the above code, we’ve represented this function as a Higher Order Kotlin function.
  • The dialog and which represents the two arguments.
  • We can improve the function by passing _ if the arguments aren’t used.
 
Object
Return Type
AlertDialog.Builder(this).create()
AlertDialog class Object for Single Button
AlertDialog.Builder(this)
AlertDialog.Builder class Object for 2 or 3 Button
⇒ visit link to view more details