Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public class

AlertDialog

extends Dialog
implements DialogInterface
java.lang.Object
   ↳ android.app.Dialog
     ↳ android.app.AlertDialog
Known Direct Subclasses

Class Overview

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 Special theme constant for AlertDialog(Context, int) : use the device's default alert theme with a dark background.
int THEME_DEVICE_DEFAULT_LIGHT Special theme constant for AlertDialog(Context, int) : use the device's default alert theme with a light background.
int THEME_HOLO_DARK Special theme constant for AlertDialog(Context, int) : use the holographic alert theme with a dark background.
int THEME_HOLO_LIGHT Special theme constant for AlertDialog(Context, int) : use the holographic alert theme with a light background.
int THEME_TRADITIONAL Special theme constant for AlertDialog(Context, int) : use the traditional (pre-Holo) alert dialog theme.
[Expand]
Inherited Constants
From interface android.content.DialogInterface
Protected Constructors
AlertDialog ( Context context)
AlertDialog ( Context context, int theme)
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)
A key was pressed down.
boolean onKeyUp (int keyCode, KeyEvent event)
A key was released.
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, DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
void setButton ( CharSequence text, DialogInterface.OnClickListener listener)
void setButton (int whichButton, CharSequence text, Message msg)
Set a message to be sent when a button is pressed.
void setButton2 ( CharSequence text, DialogInterface.OnClickListener listener)
void setButton2 ( CharSequence text, Message msg)
This method was deprecated in API level 3. Use setButton(int, CharSequence, Message) with BUTTON_NEGATIVE .
void setButton3 ( CharSequence text, DialogInterface.OnClickListener listener)
void setButton3 ( CharSequence text, Message msg)
This method was deprecated in API level 3. Use setButton(int, CharSequence, Message) with BUTTON_NEUTRAL .
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) .
[Expand]
Inherited Methods
From class android.app.Dialog
From class java.lang.Object
From interface android.content.DialogInterface
From interface android.view.KeyEvent.Callback
From interface android.view.View.OnCreateContextMenuListener
From interface android.view.Window.Callback

Constants

public static final int THEME_DEVICE_DEFAULT_DARK

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

Constant Value: 4 (0x00000004)

public static final int THEME_DEVICE_DEFAULT_LIGHT

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

Constant Value: 5 (0x00000005)

public static final int THEME_HOLO_DARK

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

Constant Value: 2 (0x00000002)

public static final int THEME_HOLO_LIGHT

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

Constant Value: 3 (0x00000003)

public static final int THEME_TRADITIONAL

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

Constant Value: 1 (0x00000001)

Protected Constructors

protected AlertDialog ( Context context)

Added in API level 1

protected AlertDialog ( Context context, int theme)

Added in API level 1

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 or one of the constants THEME_TRADITIONAL , THEME_HOLO_DARK , or THEME_HOLO_LIGHT .

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

Added in API level 1

Public Methods

public Button getButton (int whichButton)

Added in API level 3

Gets one of the buttons used in the dialog.

If a button does not exist in the dialog, null will be returned.

Parameters
whichButton The identifier of the button that should be returned. For example, this can be BUTTON_POSITIVE .
Returns
  • The button from the dialog, or null if a button does not exist.

public ListView getListView ()

Added in API level 3

Gets the list view used in the dialog.

Returns

public boolean onKeyDown (int keyCode, KeyEvent event)

Added in API level 1

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 The value in event.getKeyCode().
event Description of the key event.
Returns
  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public boolean onKeyUp (int keyCode, KeyEvent event)

Added in API level 1

A key was released.

The default implementation handles KEYCODE_BACK to close the dialog.

Parameters
keyCode The value in event.getKeyCode().
event Description of the key event.
Returns
  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public void setButton ( CharSequence text, Message msg)

Added in API level 1

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

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

Added in API level 3

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

Parameters
whichButton Which button to set the listener on, can be one of BUTTON_POSITIVE , BUTTON_NEGATIVE , or BUTTON_NEUTRAL
text The text to display in positive button.
listener The DialogInterface.OnClickListener to use.

public void setButton ( CharSequence text, DialogInterface.OnClickListener listener)

Added in API level 1

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 The text to display in button 1.
listener The DialogInterface.OnClickListener to use.

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

Added in API level 3

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

Parameters
whichButton Which button to set the message for, can be one of BUTTON_POSITIVE , BUTTON_NEGATIVE , or BUTTON_NEUTRAL
text The text to display in positive button.
msg The Message to be sent when clicked.

public void setButton2 ( CharSequence text, DialogInterface.OnClickListener listener)

Added in API level 1

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 The text to display in button 2.
listener The DialogInterface.OnClickListener to use.

public void setButton2 ( CharSequence text, Message msg)

Added in API level 1

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

public void setButton3 ( CharSequence text, DialogInterface.OnClickListener listener)

Added in API level 1

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 The text to display in button 3.
listener The DialogInterface.OnClickListener to use.

public void setButton3 ( CharSequence text, Message msg)

Added in API level 1

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

public void setCustomTitle ( View customTitleView)

Added in API level 1

public void setIcon ( Drawable icon)

Added in API level 1

public void setIcon (int resId)

Added in API level 1

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

Parameters
resId the resourceId of the drawable to use as the icon or 0 if you don't want an icon.

public void setIconAttribute (int attrId)

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

Parameters
attrId ID of a theme attribute that points to a drawable resource.

public void setInverseBackgroundForced (boolean forceInverseBackground)

Added in API level 1

public void setMessage ( CharSequence message)

Added in API level 1

public void setTitle ( CharSequence title)

Added in API level 1

Set the title text for this dialog's window.

Parameters
title The new text to display in the title.

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

Added in API level 3

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

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

public void setView ( View view)

Added in API level 1

Set the view to display in that dialog.

Protected Methods

protected void onCreate ( Bundle savedInstanceState)

Added in API level 1

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

Parameters
savedInstanceState If this dialog is being reinitalized 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.