Most visited

Recently visited

Added in API level 1

AlertDialog

public class AlertDialog
extends Dialog implements DialogInterface

java.lang.Object
   ↳ android.app.Dialog
     ↳ android.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 = (FrameLayout) 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(Bundle).

Developer Guides

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

Summary

Nested classes

class AlertDialog.Builder

 

Constants

int THEME_DEVICE_DEFAULT_DARK

This constant was deprecated in API level 23. Use Theme_DeviceDefault_Dialog_Alert.

int THEME_DEVICE_DEFAULT_LIGHT

This constant was deprecated in API level 23. Use Theme_DeviceDefault_Light_Dialog_Alert.

int THEME_HOLO_DARK

This constant was deprecated in API level 23. Use Theme_Material_Dialog_Alert.

int THEME_HOLO_LIGHT

This constant was deprecated in API level 23. Use Theme_Material_Light_Dialog_Alert.

int THEME_TRADITIONAL

This constant was deprecated in API level 23. Use Theme_Material_Dialog_Alert.

Inherited constants

From interface android.content.DialogInterface

Protected constructors

AlertDialog(Context context)

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

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

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

AlertDialog(Context context, int themeResId)

Creates an alert dialog that uses an explicit theme resource.

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)

A key was pressed down.

boolean onKeyUp(int keyCode, KeyEvent event)

A key was released.

void setButton(CharSequence text, DialogInterface.OnClickListener listener)

This method was deprecated in API level 3. Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_POSITIVE

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

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

void setButton(CharSequence text, Message msg)

This method was deprecated in API level 3. Use setButton(int, CharSequence, Message) with BUTTON_POSITIVE.

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

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

void setButton2(CharSequence text, Message msg)

This method was deprecated in API level 3. Use setButton(int, CharSequence, Message) with BUTTON_NEGATIVE.

void setButton2(CharSequence text, DialogInterface.OnClickListener listener)

This method was deprecated in API level 3. Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_NEGATIVE

void setButton3(CharSequence text, Message msg)

This method was deprecated in API level 3. Use setButton(int, CharSequence, Message) with BUTTON_NEUTRAL.

void setButton3(CharSequence text, DialogInterface.OnClickListener listener)

This method was deprecated in API level 3. Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_POSITIVE

void setCustomTitle(View customTitleView)
void setIcon(Drawable icon)
void setIcon(int resId)

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

void setIconAttribute(int attrId)

Set an icon as supplied by a theme attribute.

void setInverseBackgroundForced(boolean forceInverseBackground)
void setMessage(CharSequence message)
void setTitle(CharSequence title)

Set the title text for this dialog's window.

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

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

void setView(View view)

Set the view to display in that dialog.

Protected methods

void onCreate(Bundle savedInstanceState)

Similar to onCreate(Bundle), you should initialize your dialog in this method, including calling setContentView(View).

Inherited methods

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

Constants

THEME_DEVICE_DEFAULT_DARK

Added in API level 14
int THEME_DEVICE_DEFAULT_DARK

This constant was deprecated in API level 23.
Use Theme_DeviceDefault_Dialog_Alert.

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

Constant Value: 4 (0x00000004)

THEME_DEVICE_DEFAULT_LIGHT

Added in API level 14
int THEME_DEVICE_DEFAULT_LIGHT

This constant was deprecated in API level 23.
Use Theme_DeviceDefault_Light_Dialog_Alert.

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

Constant Value: 5 (0x00000005)

THEME_HOLO_DARK

Added in API level 11
int THEME_HOLO_DARK

This constant was deprecated in API level 23.
Use Theme_Material_Dialog_Alert.

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

Constant Value: 2 (0x00000002)

THEME_HOLO_LIGHT

Added in API level 11
int THEME_HOLO_LIGHT

This constant was deprecated in API level 23.
Use Theme_Material_Light_Dialog_Alert.

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

Constant Value: 3 (0x00000003)

THEME_TRADITIONAL

Added in API level 11
int THEME_TRADITIONAL

This constant was deprecated in API level 23.
Use Theme_Material_Dialog_Alert.

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

Constant Value: 1 (0x00000001)

Protected constructors

AlertDialog

Added in API level 1
AlertDialog (Context context)

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

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

Parameters
context Context: the parent context

See also:

AlertDialog

Added in API level 1
AlertDialog (Context context, 
                boolean cancelable, 
                DialogInterface.OnCancelListener cancelListener)

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 alertDialogTheme within the parent context's theme.

Parameters
context Context: the parent context
cancelable boolean
cancelListener DialogInterface.OnCancelListener

See also:

AlertDialog

Added in API level 1
AlertDialog (Context context, 
                int themeResId)

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

See also:

Public methods

getButton

Added in API level 3
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 API level 3
ListView getListView ()

Gets the list view used in the dialog.

Returns
ListView The ListView from the dialog.

onKeyDown

Added in API level 1
boolean onKeyDown (int keyCode, 
                KeyEvent event)

A key was pressed down.

If the focused view didn't want this event, this method is called.

The default implementation consumed the KEYCODE_BACK to later handle it in onKeyUp(int, KeyEvent).

Parameters
keyCode int: The value in event.getKeyCode().
event KeyEvent: Description of the key event.
Returns
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
boolean onKeyUp (int keyCode, 
                KeyEvent event)

A key was released.

The default implementation handles KEYCODE_BACK to close the dialog.

Parameters
keyCode int: The value in event.getKeyCode().
event KeyEvent: Description of the key event.
Returns
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 1
void setButton (CharSequence text, 
                DialogInterface.OnClickListener listener)

This method was deprecated in API level 3.
Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with 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 3
void setButton (int whichButton, 
                CharSequence text, 
                DialogInterface.OnClickListener listener)

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

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 API level 1
void setButton (CharSequence text, 
                Message msg)

This method was deprecated in API level 3.
Use setButton(int, CharSequence, Message) with BUTTON_POSITIVE.

Parameters
text CharSequence
msg Message

setButton

Added in API level 3
void setButton (int whichButton, 
                CharSequence text, 
                Message msg)

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

setButton2

Added in API level 1
void setButton2 (CharSequence text, 
                Message msg)

This method was deprecated in API level 3.
Use setButton(int, CharSequence, Message) with BUTTON_NEGATIVE.

Parameters
text CharSequence
msg Message

setButton2

Added in API level 1
void setButton2 (CharSequence text, 
                DialogInterface.OnClickListener listener)

This method was deprecated in API level 3.
Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with 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.

setButton3

Added in API level 1
void setButton3 (CharSequence text, 
                Message msg)

This method was deprecated in API level 3.
Use setButton(int, CharSequence, Message) with BUTTON_NEUTRAL.

Parameters
text CharSequence
msg Message

setButton3

Added in API level 1
void setButton3 (CharSequence text, 
                DialogInterface.OnClickListener listener)

This method was deprecated in API level 3.
Use setButton(int, CharSequence, android.content.DialogInterface.OnClickListener) with BUTTON_POSITIVE

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.

setCustomTitle

Added in API level 1
void setCustomTitle (View customTitleView)

Parameters
customTitleView View

See also:

setIcon

Added in API level 1
void setIcon (Drawable icon)

Parameters
icon Drawable

setIcon

Added in API level 1
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 API level 11
void setIconAttribute (int attrId)

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
void setInverseBackgroundForced (boolean forceInverseBackground)

Parameters
forceInverseBackground boolean

setMessage

Added in API level 1
void setMessage (CharSequence message)

Parameters
message CharSequence

setTitle

Added in API level 1
void setTitle (CharSequence title)

Set the title text for this dialog's window.

Parameters
title CharSequence: The new text to display in the title.

setView

Added in API level 3
void setView (View view, 
                int viewSpacingLeft, 
                int viewSpacingTop, 
                int viewSpacingRight, 
                int viewSpacingBottom)

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

setView

Added in API level 1
void setView (View view)

Set the view to display in that dialog.

Parameters
view View

Protected methods

onCreate

Added in API level 1
void onCreate (Bundle savedInstanceState)

Similar to onCreate(Bundle), you should initialize your dialog in this method, including calling setContentView(View).

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.

Hooray!