AlertDialog

open class AlertDialog : AppCompatDialog, DialogInterface
kotlin.Any
   ↳ android.app.Dialog
   ↳ androidx.appcompat.app.AppCompatDialog
   ↳ androidx.appcompat.app.AlertDialog

A subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method. If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:

 FrameLayout fl = findViewById(android.R.id.custom); fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); 

The AlertDialog class takes care of automatically setting android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM for you based on whether any views in the dialog return true from View.onCheckIsTextEditor(). Generally you want this set for a Dialog without text editors, so that it will be placed on top of the current input method UI. You can modify this behavior by forcing the flag to your desired mode after calling onCreate.

Developer Guides

For more information about creating dialogs, read the Dialogs developer guide.

Summary

Nested classes
open

Protected constructors
<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @StyleRes themeResId: Int)

Construct an AlertDialog that uses an explicit theme.

<init>(@NonNull context: Context, cancelable: Boolean, @Nullable cancelListener: OnCancelListener?)

Public methods
open Boolean
onKeyDown(keyCode: Int, event: KeyEvent!)

open Unit

Sets an icon as supplied by a theme attribute.

open Unit
setIcon(resId: Int)

Set resId to 0 if you don't want an icon.

open Unit
setIcon(icon: Drawable!)

Set the Drawable to be used in the title.

open Unit

Sets the message to display.

open Unit
setView(view: View!)

Set the view to display in the dialog.

open Unit
setView(view: View!, viewSpacingLeft: Int, viewSpacingTop: Int, viewSpacingRight: Int, viewSpacingBottom: Int)

Set the view to display in the dialog, specifying the spacing to appear around that view.

open Unit
setCustomTitle(customTitleView: View!)

This method has no effect if called after show().

open Unit

open ListView!

Gets the list view used in the dialog.

open Unit
setButton(whichButton: Int, text: CharSequence!, msg: Message!)

Sets a message to be sent when a button is pressed.

open Unit
setButton(whichButton: Int, text: CharSequence!, listener: OnClickListener!)

Sets a listener to be invoked when the positive button of the dialog is pressed.

open Unit
setButton(whichButton: Int, text: CharSequence!, icon: Drawable!, listener: OnClickListener!)

Sets an icon to be displayed along with the button text and a listener to be invoked when the positive button of the dialog is pressed.

open Boolean
onKeyUp(keyCode: Int, event: KeyEvent!)

open Button!
getButton(whichButton: Int)

Gets one of the buttons used in the dialog.

Protected methods
open Unit
onCreate(savedInstanceState: Bundle!)

Inherited functions

Protected constructors

<init>

protected AlertDialog(@NonNull context: Context)

<init>

protected AlertDialog(@NonNull context: Context, @StyleRes themeResId: Int)

Construct an AlertDialog that uses an explicit theme. The actual style that an AlertDialog uses is a private implementation, however you can here supply either the name of an attribute in the theme from which to get the dialog's style (such as R.attr#alertDialogTheme.

<init>

protected AlertDialog(@NonNull context: Context, cancelable: Boolean, @Nullable cancelListener: OnCancelListener?)

Public methods

onKeyDown

open fun onKeyDown(keyCode: Int, event: KeyEvent!): Boolean

setIconAttribute

open fun setIconAttribute(attrId: Int): Unit

Sets an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon

Parameters
attrId Int: ID of a theme attribute that points to a drawable resource.

setIcon

open fun setIcon(resId: Int): Unit

Set resId to 0 if you don't want an icon.

Parameters
resId Int: the resourceId of the drawable to use as the icon or 0 if you don't want an icon.

setIcon

open fun setIcon(icon: Drawable!): Unit

Set the Drawable to be used in the title.

Parameters
icon Drawable!: Drawable to use as the icon or null if you don't want an icon.

setMessage

open fun setMessage(message: CharSequence!): Unit

Sets the message to display.

Parameters
message CharSequence!: The message to display in the dialog.

setView

open fun setView(view: View!): Unit

Set the view to display in the dialog. This method has no effect if called after show().

setView

open fun setView(view: View!, viewSpacingLeft: Int, viewSpacingTop: Int, viewSpacingRight: Int, viewSpacingBottom: Int): Unit

Set the view to display in the dialog, specifying the spacing to appear around that view. This method has no effect if called after show().

Parameters
view View!: The view to show in the content area of the dialog
viewSpacingLeft View!: Extra space to appear to the left of view
viewSpacingTop View!: Extra space to appear above view
viewSpacingRight View!: Extra space to appear to the right of view
viewSpacingBottom View!: Extra space to appear below view

setCustomTitle

open fun setCustomTitle(customTitleView: View!): Unit

This method has no effect if called after show().

setTitle

open fun setTitle(title: CharSequence!): Unit

getListView

open fun getListView(): ListView!

Gets the list view used in the dialog.

Return
ListView!: The ListView from the dialog.

setButton

open fun setButton(whichButton: Int, text: CharSequence!, msg: Message!): Unit

Sets a message to be sent when a button is pressed. This method has no effect if called after show().

Parameters
whichButton Int: Which button to set the message for, can be one of DialogInterface#BUTTON_POSITIVE, DialogInterface#BUTTON_NEGATIVE, or DialogInterface#BUTTON_NEUTRAL
text Int: The text to display in positive button.
msg Int: The Message to be sent when clicked.

setButton

open fun setButton(whichButton: Int, text: CharSequence!, listener: OnClickListener!): Unit

Sets a listener to be invoked when the positive button of the dialog is pressed. This method has no effect if called after show().

Parameters
whichButton Int: Which button to set the listener on, can be one of DialogInterface#BUTTON_POSITIVE, DialogInterface#BUTTON_NEGATIVE, or DialogInterface#BUTTON_NEUTRAL
text Int: The text to display in positive button.
listener Int: The DialogInterface.OnClickListener to use.

setButton

open fun setButton(whichButton: Int, text: CharSequence!, icon: Drawable!, listener: OnClickListener!): Unit

Sets an icon to be displayed along with the button text and a listener to be invoked when the positive button of the dialog is pressed. This method has no effect if called after show().

Parameters
whichButton Int: Which button to set the listener on, can be one of DialogInterface#BUTTON_POSITIVE, DialogInterface#BUTTON_NEGATIVE, or DialogInterface#BUTTON_NEUTRAL
text Int: The text to display in positive button.
listener Int: The DialogInterface.OnClickListener to use.
icon Int: The Drawable to be set as an icon for the button.

onKeyUp

open fun onKeyUp(keyCode: Int, event: KeyEvent!): Boolean

getButton

open fun getButton(whichButton: Int): Button!

Gets one of the buttons used in the dialog. Returns null if the specified button does not exist or the dialog has not yet been fully created (for example, via show() or create()).

Parameters
whichButton Int: The identifier of the button that should be returned. For example, this can be DialogInterface#BUTTON_POSITIVE.
Return
Button!: The button from the dialog, or null if a button does not exist.

Protected methods

onCreate

protected open fun onCreate(savedInstanceState: Bundle!): Unit