AlertDialog


class AlertDialog : AppCompatDialog, DialogInterface

Known direct subclasses
MediaRouteControllerDialog

This class implements the route controller dialog for MediaRouter.


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 types

Protected constructors

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

Construct an AlertDialog that uses an explicit theme.

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

Public functions

Button!
getButton(whichButton: Int)

Gets one of the buttons used in the dialog.

ListView!

Gets the list view used in the dialog.

Boolean
onKeyDown(keyCode: Int, event: KeyEvent!)
Boolean
onKeyUp(keyCode: Int, event: KeyEvent!)
Unit
setButton(
    whichButton: Int,
    text: CharSequence!,
    listener: DialogInterface.OnClickListener!
)

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

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

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

Unit
setButton(
    whichButton: Int,
    text: CharSequence!,
    icon: Drawable!,
    listener: DialogInterface.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.

Unit
setCustomTitle(customTitleView: View!)

This method has no effect if called after show.

Unit
setIcon(icon: Drawable!)

Set the Drawable to be used in the title.

Unit
setIcon(resId: Int)

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

Unit

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

Unit

Sets the message to display.

Unit
Unit
setView(view: View!)

Set the view to display in the dialog.

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 functions

Unit
onCreate(savedInstanceState: Bundle!)

Inherited Constants

From android.content.DialogInterface
const Int
BUTTON1 = -1

This property is deprecated.

const Int
BUTTON2 = -2

This property is deprecated.

const Int
BUTTON3 = -3

This property is deprecated.

const Int
const Int
const Int

Inherited functions

From androidx.appcompat.app.AppCompatDialog
Unit
Unit
Boolean
T?
<T : View?> findViewById(id: @IdRes Int)
AppCompatDelegate
ActionBar!

Support library version of getActionBar.

Unit
Unit

Called when a support action mode has finished.

Unit

Called when a support action mode has been started.

ActionMode?

Called when a support action mode is being started for this window.

Unit
setContentView(layoutResID: @LayoutRes Int)
Unit
Unit
Unit
setTitle(titleId: Int)
Boolean

Enable extended support library window features.

From androidx.activity.ComponentDialog
Unit
Lifecycle!
SavedStateRegistry!
Unit

Sets the view tree owners before setting the content view so that the inflation process and attach listeners will see them already present.

Unit

This function is deprecated.

Unit
onCreate(savedInstanceState: Bundle!)
Bundle!
Unit
Unit
Unit
setContentView(layoutResID: Int)
From android.app.Dialog
Unit
Unit
Unit
Boolean
Boolean
Boolean
Boolean
Boolean
Boolean
T!
<T : View?> findViewById(id: Int)
ActionBar!
Context!
View!
LayoutInflater!
OnBackInvokedDispatcher!
Activity!
SearchEvent!
Int
Window!
Unit
Unit
Boolean
Unit
Unit
Unit
Unit

This function is deprecated.

Unit
Boolean
Unit
Unit
onCreate(savedInstanceState: Bundle!)
Unit
onCreateContextMenu(
    menu: ContextMenu!,
    v: View!,
    menuInfo: ContextMenu.ContextMenuInfo!
)
Boolean
Boolean
onCreatePanelMenu(featureId: Int, menu: Menu!)
View!
onCreatePanelView(featureId: Int)
Unit
Boolean
Boolean
onKeyLongPress(keyCode: Int, event: KeyEvent!)
Boolean
onKeyMultiple(keyCode: Int, repeatCount: Int, event: KeyEvent!)
Boolean
onKeyShortcut(keyCode: Int, event: KeyEvent!)
Boolean
onMenuItemSelected(featureId: Int, item: MenuItem!)
Boolean
onMenuOpened(featureId: Int, menu: Menu!)
Boolean
Unit
Unit
onPanelClosed(featureId: Int, menu: Menu!)
Boolean
Boolean
onPreparePanel(featureId: Int, view: View!, menu: Menu!)
Unit
onRestoreInstanceState(savedInstanceState: Bundle!)
Bundle!
Boolean
Unit
Unit
Boolean
Boolean
Unit
Unit
ActionMode!
Unit
Unit
Unit
Boolean
T!
<T : View?> requireViewById(id: Int)
Unit
Unit
Unit
Unit
setContentView(layoutResID: Int)
Unit
Unit
setFeatureDrawable(featureId: Int, drawable: Drawable!)
Unit
setFeatureDrawableAlpha(featureId: Int, alpha: Int)
Unit
setFeatureDrawableResource(featureId: Int, resId: Int)
Unit
setFeatureDrawableUri(featureId: Int, uri: Uri!)
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit
From android.content.DialogInterface
abstract Unit
abstract Unit
From androidx.lifecycle.LifecycleOwner
abstract Lifecycle!
From androidx.savedstate.SavedStateRegistryOwner
From android.view.Window.Callback
Unit
Unit
onProvideKeyboardShortcuts(
    data: (Mutable)List<KeyboardShortcutGroup!>!,
    menu: Menu!,
    deviceId: Int
)

Inherited properties

From androidx.activity.ComponentDialog
OnBackPressedDispatcher!

The OnBackPressedDispatcher that should handle the system back button.

Protected constructors

AlertDialog

Added in 1.1.0
protected AlertDialog(context: Context)

AlertDialog

Added in 1.1.0
protected AlertDialog(context: Context, themeResId: @StyleRes 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 alertDialogTheme.

AlertDialog

Added in 1.1.0
protected AlertDialog(
    context: Context,
    cancelable: Boolean,
    cancelListener: DialogInterface.OnCancelListener?
)

Public functions

getButton

Added in 1.1.0
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 BUTTON_POSITIVE.

Returns
Button!

The button from the dialog, or null if a button does not exist.

getListView

Added in 1.1.0
fun getListView(): ListView!

Gets the list view used in the dialog.

Returns
ListView!

The ListView from the dialog.

onKeyDown

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

onKeyUp

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

setButton

Added in 1.1.0
fun setButton(
    whichButton: Int,
    text: CharSequence!,
    listener: DialogInterface.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 BUTTON_POSITIVE, BUTTON_NEGATIVE, or BUTTON_NEUTRAL

text: CharSequence!

The text to display in positive button.

listener: DialogInterface.OnClickListener!

The DialogInterface.OnClickListener to use.

setButton

Added in 1.1.0
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 BUTTON_POSITIVE, BUTTON_NEGATIVE, or BUTTON_NEUTRAL

text: CharSequence!

The text to display in positive button.

msg: Message!

The Message to be sent when clicked.

setButton

Added in 1.1.0
fun setButton(
    whichButton: Int,
    text: CharSequence!,
    icon: Drawable!,
    listener: DialogInterface.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 BUTTON_POSITIVE, BUTTON_NEGATIVE, or BUTTON_NEUTRAL

text: CharSequence!

The text to display in positive button.

icon: Drawable!

The Drawable to be set as an icon for the button.

listener: DialogInterface.OnClickListener!

The DialogInterface.OnClickListener to use.

setCustomTitle

Added in 1.1.0
fun setCustomTitle(customTitleView: View!): Unit

This method has no effect if called after show.

See also
setCustomTitle

setIcon

Added in 1.1.0
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.

setIcon

Added in 1.1.0
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 1.1.0
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

Added in 1.1.0
fun setMessage(message: CharSequence!): Unit

Sets the message to display.

Parameters
message: CharSequence!

The message to display in the dialog.

setTitle

fun setTitle(title: CharSequence!): Unit

setView

Added in 1.1.0
fun setView(view: View!): Unit

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

setView

Added in 1.1.0
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: 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 functions

onCreate

protected fun onCreate(savedInstanceState: Bundle!): Unit