Added in API level 1

AlertDialog


open class AlertDialog : Dialog, DialogInterface
kotlin.Any
   ↳ android.app.Dialog
   ↳ android.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 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

Constants
static Int

Special theme constant for AlertDialog(android.content.Context,int): use the device's default alert theme with a dark background.

static Int

Special theme constant for AlertDialog(android.content.Context,int): use the device's default alert theme with a light background.

static Int

Special theme constant for AlertDialog(android.content.Context,int): use the holographic alert theme with a dark background.

static Int

Special theme constant for AlertDialog(android.content.Context,int): use the holographic alert theme with a light background.

static Int

Special theme constant for AlertDialog(android.content.Context,int): use the traditional (pre-Holo) alert dialog theme.

Inherited constants
Protected constructors
AlertDialog(context: Context!)

Creates an alert dialog that uses the default alert dialog theme.

AlertDialog(context: Context!, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener!)

Creates an alert dialog that uses the default alert dialog theme and a custom cancel listener.

AlertDialog(context: Context!, themeResId: Int)

Creates an alert dialog that uses an explicit theme resource.

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!, listener: DialogInterface.OnClickListener!)

Set a listener to be invoked when the specified button of the dialog is pressed.

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

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

open Unit

Set a listener to be invoked when button 1 of the dialog is pressed.

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

open Unit

Set a listener to be invoked when button 2 of the dialog is pressed.

open Unit

open Unit

Set a listener to be invoked when button 3 of the dialog is pressed.

open Unit

open Unit
setCustomTitle(customTitleView: View!)

open Unit
setIcon(icon: Drawable!)

open Unit
setIcon(resId: Int)

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

open Unit

Set an icon as supplied by a theme attribute.

open Unit
setInverseBackgroundForced(forceInverseBackground: Boolean)

open Unit

open Unit

open Unit
setView(view: View!)

Set the view to display in that dialog.

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

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

Protected methods
open Unit
onCreate(savedInstanceState: Bundle!)

Inherited functions

Constants

THEME_DEVICE_DEFAULT_DARK

Added in API level 14
Deprecated in API level 23
static val THEME_DEVICE_DEFAULT_DARK: Int

Deprecated: Use android.R.style#Theme_DeviceDefault_Dialog_Alert.

Special theme constant for AlertDialog(android.content.Context,int): use the device's default alert theme with a dark background.

Value: 4

THEME_DEVICE_DEFAULT_LIGHT

Added in API level 14
Deprecated in API level 23
static val THEME_DEVICE_DEFAULT_LIGHT: Int

Deprecated: Use android.R.style#Theme_DeviceDefault_Light_Dialog_Alert.

Special theme constant for AlertDialog(android.content.Context,int): use the device's default alert theme with a light background.

Value: 5

THEME_HOLO_DARK

Added in API level 11
Deprecated in API level 23
static val THEME_HOLO_DARK: Int

Deprecated: Use android.R.style#Theme_Material_Dialog_Alert.

Special theme constant for AlertDialog(android.content.Context,int): use the holographic alert theme with a dark background.

Value: 2

THEME_HOLO_LIGHT

Added in API level 11
Deprecated in API level 23
static val THEME_HOLO_LIGHT: Int

Deprecated: Use android.R.style#Theme_Material_Light_Dialog_Alert.

Special theme constant for AlertDialog(android.content.Context,int): use the holographic alert theme with a light background.

Value: 3

THEME_TRADITIONAL

Added in API level 11
Deprecated in API level 23
static val THEME_TRADITIONAL: Int

Deprecated: Use android.R.style#Theme_Material_Dialog_Alert.

Special theme constant for AlertDialog(android.content.Context,int): use the traditional (pre-Holo) alert dialog theme.

Value: 1

Protected constructors

AlertDialog

Added in API level 1
protected AlertDialog(context: Context!)

Creates an alert dialog that uses the default alert dialog theme.

The default alert dialog theme is defined by android.R.attr#alertDialogTheme within the parent context's theme.

Parameters
context Context!: the parent context

AlertDialog

Added in API level 1
protected AlertDialog(
    context: Context!,
    cancelable: Boolean,
    cancelListener: DialogInterface.OnCancelListener!)

Creates an alert dialog that uses the default alert dialog theme and a custom cancel listener.

This is functionally identical to:

AlertDialog dialog = new AlertDialog(context);
      alertDialog.setCancelable(cancelable);
      alertDialog.setOnCancelListener(cancelListener);
  

The default alert dialog theme is defined by android.R.attr#alertDialogTheme within the parent context's theme.

Parameters
context Context!: the parent context

AlertDialog

Added in API level 1
protected AlertDialog(
    context: Context!,
    themeResId: Int)

Creates an alert dialog that uses an explicit theme resource.

The specified theme resource (themeResId) is applied on top of the parent context's theme. It may be specified as a style resource containing a fully-populated theme, such as android.R.style#Theme_Material_Dialog, to replace all attributes in the parent context's theme including primary and accent colors.

To preserve attributes such as primary and accent colors, the themeResId may instead be specified as an overlay theme such as android.R.style#ThemeOverlay_Material_Dialog. This will override only the window attributes necessary to style the alert window as a dialog.

Alternatively, the themeResId may be specified as 0 to use the parent context's resolved value for android.R.attr#alertDialogTheme.

Parameters
context Context!: the parent context
themeResId Int: the resource ID of the theme against which to inflate this dialog, or 0 to use the parent context's default alert dialog theme

Public methods

getButton

Added in API level 3
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

Added in API level 3
open fun getListView(): ListView!

Gets the list view used in the dialog.

Return
ListView! The ListView from the dialog.

onKeyDown

Added in API level 1
open fun onKeyDown(
    keyCode: Int,
    event: KeyEvent
): Boolean
Parameters
keyCode Int: The value in event.getKeyCode().
event KeyEvent: This value cannot be null.
Return
Boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

onKeyUp

Added in API level 1
open fun onKeyUp(
    keyCode: Int,
    event: KeyEvent
): Boolean
Parameters
keyCode Int: The value in event.getKeyCode().
event KeyEvent: This value cannot be null.
Return
Boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

setButton

Added in API level 3
open fun setButton(
    whichButton: Int,
    text: CharSequence!,
    listener: DialogInterface.OnClickListener!
): Unit

Set a listener to be invoked when the specified button of the dialog is pressed.

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 CharSequence!: The text to display in positive button.
listener DialogInterface.OnClickListener!: The DialogInterface.OnClickListener to use.

setButton

Added in API level 3
open fun setButton(
    whichButton: Int,
    text: CharSequence!,
    msg: Message!
): Unit

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

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 CharSequence!: The text to display in positive button.
msg Message!: The Message to be sent when clicked.

setButton

Added in API level 1
Deprecated in API level 15
open fun setButton(
    text: CharSequence!,
    listener: DialogInterface.OnClickListener!
): Unit

Deprecated: Use setButton(int,java.lang.CharSequence,android.content.DialogInterface.OnClickListener) with DialogInterface.BUTTON_POSITIVE

Set a listener to be invoked when button 1 of the dialog is pressed.

Parameters
text CharSequence!: The text to display in button 1.
listener DialogInterface.OnClickListener!: The DialogInterface.OnClickListener to use.

setButton

Added in API level 1
Deprecated in API level 15
open fun setButton(
    text: CharSequence!,
    msg: Message!
): Unit

Deprecated: Use setButton(int,java.lang.CharSequence,android.os.Message) with DialogInterface.BUTTON_POSITIVE.

setButton2

Added in API level 1
Deprecated in API level 15
open fun setButton2(
    text: CharSequence!,
    listener: DialogInterface.OnClickListener!
): Unit

Deprecated: Use setButton(int,java.lang.CharSequence,android.content.DialogInterface.OnClickListener) with DialogInterface.BUTTON_NEGATIVE

Set a listener to be invoked when button 2 of the dialog is pressed.

Parameters
text CharSequence!: The text to display in button 2.
listener DialogInterface.OnClickListener!: The DialogInterface.OnClickListener to use.

setButton2

Added in API level 1
Deprecated in API level 15
open fun setButton2(
    text: CharSequence!,
    msg: Message!
): Unit

Deprecated: Use setButton(int,java.lang.CharSequence,android.os.Message) with DialogInterface.BUTTON_NEGATIVE.

setButton3

Added in API level 1
Deprecated in API level 15
open fun setButton3(
    text: CharSequence!,
    listener: DialogInterface.OnClickListener!
): Unit

Deprecated: Use setButton(int,java.lang.CharSequence,android.content.DialogInterface.OnClickListener) with DialogInterface.BUTTON_NEUTRAL

Set a listener to be invoked when button 3 of the dialog is pressed.

Parameters
text CharSequence!: The text to display in button 3.
listener DialogInterface.OnClickListener!: The DialogInterface.OnClickListener to use.

setButton3

Added in API level 1
Deprecated in API level 15
open fun setButton3(
    text: CharSequence!,
    msg: Message!
): Unit

Deprecated: Use setButton(int,java.lang.CharSequence,android.os.Message) with DialogInterface.BUTTON_NEUTRAL.

setCustomTitle

Added in API level 1
open fun setCustomTitle(customTitleView: View!): Unit

setIcon

Added in API level 1
open fun setIcon(icon: Drawable!): Unit

setIcon

Added in API level 1
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.

setIconAttribute

Added in API level 11
open fun setIconAttribute(attrId: Int): Unit

Set 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.

setInverseBackgroundForced

Added in API level 1
open fun setInverseBackgroundForced(forceInverseBackground: Boolean): Unit

setMessage

Added in API level 1
open fun setMessage(message: CharSequence!): Unit

setTitle

Added in API level 1
open fun setTitle(title: CharSequence?): Unit
Parameters
title CharSequence?: The new text to display in the title. This value may be null.

setView

Added in API level 1
open fun setView(view: View!): Unit

Set the view to display in that dialog.

setView

Added in API level 3
open fun setView(
    view: View!,
    viewSpacingLeft: Int,
    viewSpacingTop: Int,
    viewSpacingRight: Int,
    viewSpacingBottom: Int
): Unit

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

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

Protected methods

onCreate

Added in API level 1
protected open fun onCreate(savedInstanceState: Bundle!): Unit
Parameters
savedInstanceState Bundle!: If this dialog is being reinitialized after a the hosting activity was previously shut down, holds the result from the most recent call to onSaveInstanceState, or null if this is the first time.