The Android Developer Challenge is back! Submit your idea before December 2.

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.

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 Button!
getButton(whichButton: Int)

Gets one of the buttons used in the dialog.

open ListView!

Gets the list view used in the dialog.

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

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

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 Unit
setCustomTitle(customTitleView: View!)

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

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 an icon as supplied by a theme attribute.

open Unit

Sets the message to display.

open Unit

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.

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

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.

getListView

open fun getListView(): ListView!

Gets the list view used in the dialog.

Return
ListView!: The ListView from the dialog.

onKeyDown

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

onKeyUp

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

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.

setCustomTitle

open fun setCustomTitle(customTitleView: View!): Unit

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

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.

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.

setMessage

open fun setMessage(message: CharSequence!): Unit

Sets the message to display.

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

setTitle

open fun setTitle(title: CharSequence!): Unit

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

Protected methods

onCreate

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