added in version 24.1.0
belongs to Maven artifact com.android.support:appcompat-v7:28.0.0-alpha1

AlertDialog

public class AlertDialog
extends AppCompatDialog implements DialogInterface

java.lang.Object
   ↳ android.app.Dialog
     ↳ android.support.v7.app.AppCompatDialog
       ↳ android.support.v7.app.AlertDialog
Known Direct Subclasses


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(Bundle).

Developer Guides

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

Summary

Nested classes

class AlertDialog.Builder

 

Inherited constants

From interface android.content.DialogInterface

Protected constructors

AlertDialog(Context context)
AlertDialog(Context context, int themeResId)

Construct an AlertDialog that uses an explicit theme.

AlertDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)

Public methods

Button getButton(int whichButton)

Gets one of the buttons used in the dialog.

ListView getListView()

Gets the list view used in the dialog.

boolean onKeyDown(int keyCode, KeyEvent event)
boolean onKeyUp(int keyCode, KeyEvent event)
void setButton(int whichButton, CharSequence text, Drawable icon, DialogInterface.OnClickListener listener)

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.

void setButton(int whichButton, CharSequence text, DialogInterface.OnClickListener listener)

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

void setButton(int whichButton, CharSequence text, Message msg)

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

void setCustomTitle(View customTitleView)

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

void setIcon(Drawable icon)

Set the Drawable to be used in the title.

void setIcon(int resId)

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

void setIconAttribute(int attrId)

Sets an icon as supplied by a theme attribute.

void setMessage(CharSequence message)

Sets the message to display.

void setTitle(CharSequence title)
void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)

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

void setView(View view)

Set the view to display in the dialog.

Protected methods

void onCreate(Bundle savedInstanceState)

Inherited methods

From class android.support.v7.app.AppCompatDialog
From class android.app.Dialog
From class java.lang.Object
From interface android.support.v7.app.AppCompatCallback
From interface android.content.DialogInterface
From interface android.view.Window.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.View.OnCreateContextMenuListener

Protected constructors

AlertDialog

added in version 24.1.0
AlertDialog (Context context)

Parameters
context Context

AlertDialog

added in version 24.1.0
AlertDialog (Context context, 
                int themeResId)

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.

Parameters
context Context

themeResId int

AlertDialog

added in version 24.1.0
AlertDialog (Context context, 
                boolean cancelable, 
                DialogInterface.OnCancelListener cancelListener)

Parameters
context Context

cancelable boolean

cancelListener DialogInterface.OnCancelListener

Public methods

getButton

added in version 24.1.0
Button getButton (int whichButton)

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 version 24.1.0
ListView getListView ()

Gets the list view used in the dialog.

Returns
ListView The ListView from the dialog.

onKeyDown

boolean onKeyDown (int keyCode, 
                KeyEvent event)

Parameters
keyCode int

event KeyEvent

Returns
boolean

onKeyUp

boolean onKeyUp (int keyCode, 
                KeyEvent event)

Parameters
keyCode int

event KeyEvent

Returns
boolean

setButton

added in version 27.1.0
void setButton (int whichButton, 
                CharSequence text, 
                Drawable icon, 
                DialogInterface.OnClickListener listener)

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.

setButton

added in version 24.1.0
void setButton (int whichButton, 
                CharSequence text, 
                DialogInterface.OnClickListener listener)

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 version 24.1.0
void setButton (int whichButton, 
                CharSequence text, 
                Message msg)

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.

setCustomTitle

added in version 24.1.0
void setCustomTitle (View customTitleView)

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

Parameters
customTitleView View

setIcon

added in version 24.1.0
void setIcon (Drawable icon)

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 version 24.1.0
void setIcon (int resId)

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 version 24.1.0
void setIconAttribute (int attrId)

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 version 24.1.0
void setMessage (CharSequence message)

Sets the message to display.

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

setTitle

void setTitle (CharSequence title)

Parameters
title CharSequence

setView

added in version 24.1.0
void setView (View view, 
                int viewSpacingLeft, 
                int viewSpacingTop, 
                int viewSpacingRight, 
                int viewSpacingBottom)

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

setView

added in version 24.1.0
void setView (View view)

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

Parameters
view View

Protected methods

onCreate

void onCreate (Bundle savedInstanceState)

Parameters
savedInstanceState Bundle