FileDocCategorySizeDatePackage
AlertDialog.javaAPI DocAndroid 5.1 API42338Thu Mar 12 22:22:10 GMT 2015android.app

AlertDialog

public class AlertDialog extends Dialog implements android.content.DialogInterface
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 {@link WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} for you based on whether any views in the dialog return true from {@link View#onCheckIsTextEditor() 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 {@link #onCreate}.

Developer Guides

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

Fields Summary
private com.android.internal.app.AlertController
mAlert
public static final int
THEME_TRADITIONAL
Special theme constant for {@link #AlertDialog(Context, int)}: use the traditional (pre-Holo) alert dialog theme.
public static final int
THEME_HOLO_DARK
Special theme constant for {@link #AlertDialog(Context, int)}: use the holographic alert theme with a dark background.
public static final int
THEME_HOLO_LIGHT
Special theme constant for {@link #AlertDialog(Context, int)}: use the holographic alert theme with a light background.
public static final int
THEME_DEVICE_DEFAULT_DARK
Special theme constant for {@link #AlertDialog(Context, int)}: use the device's default alert theme with a dark background.
public static final int
THEME_DEVICE_DEFAULT_LIGHT
Special theme constant for {@link #AlertDialog(Context, int)}: use the device's default alert theme with a light background.
public static final int
LAYOUT_HINT_NONE
No layout hint.
public static final int
LAYOUT_HINT_SIDE
Hint layout to the side.
Constructors Summary
protected AlertDialog(android.content.Context context)

    
       
        this(context, resolveDialogTheme(context, 0), true);
    
protected AlertDialog(android.content.Context context, int theme)
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 {@link android.R.attr#alertDialogTheme} or one of the constants {@link #THEME_TRADITIONAL}, {@link #THEME_HOLO_DARK}, or {@link #THEME_HOLO_LIGHT}.

        this(context, theme, true);
    
AlertDialog(android.content.Context context, int theme, boolean createThemeContextWrapper)

        super(context, resolveDialogTheme(context, theme), createThemeContextWrapper);

        mWindow.alwaysReadCloseOnTouchAttr();
        mAlert = new AlertController(getContext(), this, getWindow());
    
protected AlertDialog(android.content.Context context, boolean cancelable, OnCancelListener cancelListener)

        super(context, resolveDialogTheme(context, 0));
        mWindow.alwaysReadCloseOnTouchAttr();
        setCancelable(cancelable);
        setOnCancelListener(cancelListener);
        mAlert = new AlertController(context, this, getWindow());
    
Methods Summary
public android.widget.ButtongetButton(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 {@link #show()} or {@link #create()}).

param
whichButton The identifier of the button that should be returned. For example, this can be {@link DialogInterface#BUTTON_POSITIVE}.
return
The button from the dialog, or null if a button does not exist.

        return mAlert.getButton(whichButton);
    
public android.widget.ListViewgetListView()
Gets the list view used in the dialog.

return
The {@link ListView} from the dialog.

        return mAlert.getListView();
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);
        mAlert.installContent();
    
public booleanonKeyDown(int keyCode, android.view.KeyEvent event)

        if (mAlert.onKeyDown(keyCode, event)) return true;
        return super.onKeyDown(keyCode, event);
    
public booleanonKeyUp(int keyCode, android.view.KeyEvent event)

        if (mAlert.onKeyUp(keyCode, event)) return true;
        return super.onKeyUp(keyCode, event);
    
static intresolveDialogTheme(android.content.Context context, int resid)

        if (resid == THEME_TRADITIONAL) {
            return com.android.internal.R.style.Theme_Dialog_Alert;
        } else if (resid == THEME_HOLO_DARK) {
            return com.android.internal.R.style.Theme_Holo_Dialog_Alert;
        } else if (resid == THEME_HOLO_LIGHT) {
            return com.android.internal.R.style.Theme_Holo_Light_Dialog_Alert;
        } else if (resid == THEME_DEVICE_DEFAULT_DARK) {
            return com.android.internal.R.style.Theme_DeviceDefault_Dialog_Alert;
        } else if (resid == THEME_DEVICE_DEFAULT_LIGHT) {
            return com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog_Alert;
        } else if (resid >= 0x01000000) {   // start of real resource IDs.
            return resid;
        } else {
            TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme,
                    outValue, true);
            return outValue.resourceId;
        }
    
public voidsetButton(int whichButton, java.lang.CharSequence text, android.os.Message msg)
Set a message to be sent when a button is pressed.

param
whichButton Which button to set the message for, can be one of {@link DialogInterface#BUTTON_POSITIVE}, {@link DialogInterface#BUTTON_NEGATIVE}, or {@link DialogInterface#BUTTON_NEUTRAL}
param
text The text to display in positive button.
param
msg The {@link Message} to be sent when clicked.

        mAlert.setButton(whichButton, text, null, msg);
    
public voidsetButton(int whichButton, java.lang.CharSequence text, OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.

param
whichButton Which button to set the listener on, can be one of {@link DialogInterface#BUTTON_POSITIVE}, {@link DialogInterface#BUTTON_NEGATIVE}, or {@link DialogInterface#BUTTON_NEUTRAL}
param
text The text to display in positive button.
param
listener The {@link DialogInterface.OnClickListener} to use.

        mAlert.setButton(whichButton, text, listener, null);
    
public voidsetButton(java.lang.CharSequence text, android.os.Message msg)

deprecated
Use {@link #setButton(int, CharSequence, Message)} with {@link DialogInterface#BUTTON_POSITIVE}.

        setButton(BUTTON_POSITIVE, text, msg);
    
public voidsetButton(java.lang.CharSequence text, OnClickListener listener)
Set a listener to be invoked when button 1 of the dialog is pressed.

param
text The text to display in button 1.
param
listener The {@link DialogInterface.OnClickListener} to use.
deprecated
Use {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} with {@link DialogInterface#BUTTON_POSITIVE}

        setButton(BUTTON_POSITIVE, text, listener);
    
public voidsetButton2(java.lang.CharSequence text, android.os.Message msg)

deprecated
Use {@link #setButton(int, CharSequence, Message)} with {@link DialogInterface#BUTTON_NEGATIVE}.

        setButton(BUTTON_NEGATIVE, text, msg);
    
public voidsetButton2(java.lang.CharSequence text, OnClickListener listener)
Set a listener to be invoked when button 2 of the dialog is pressed.

param
text The text to display in button 2.
param
listener The {@link DialogInterface.OnClickListener} to use.
deprecated
Use {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} with {@link DialogInterface#BUTTON_NEGATIVE}

        setButton(BUTTON_NEGATIVE, text, listener);
    
public voidsetButton3(java.lang.CharSequence text, android.os.Message msg)

deprecated
Use {@link #setButton(int, CharSequence, Message)} with {@link DialogInterface#BUTTON_NEUTRAL}.

        setButton(BUTTON_NEUTRAL, text, msg);
    
public voidsetButton3(java.lang.CharSequence text, OnClickListener listener)
Set a listener to be invoked when button 3 of the dialog is pressed.

param
text The text to display in button 3.
param
listener The {@link DialogInterface.OnClickListener} to use.
deprecated
Use {@link #setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)} with {@link DialogInterface#BUTTON_POSITIVE}

        setButton(BUTTON_NEUTRAL, text, listener);
    
voidsetButtonPanelLayoutHint(int layoutHint)
Internal api to allow hinting for the best button panel layout.

hide

        mAlert.setButtonPanelLayoutHint(layoutHint);
    
public voidsetCustomTitle(android.view.View customTitleView)

see
Builder#setCustomTitle(View)

        mAlert.setCustomTitle(customTitleView);
    
public voidsetIcon(int resId)
Set resId to 0 if you don't want an icon.

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

        mAlert.setIcon(resId);
    
public voidsetIcon(android.graphics.drawable.Drawable icon)

        mAlert.setIcon(icon);
    
public voidsetIconAttribute(int attrId)
Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon

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

        TypedValue out = new TypedValue();
        mContext.getTheme().resolveAttribute(attrId, out, true);
        mAlert.setIcon(out.resourceId);
    
public voidsetInverseBackgroundForced(boolean forceInverseBackground)

        mAlert.setInverseBackgroundForced(forceInverseBackground);
    
public voidsetMessage(java.lang.CharSequence message)

        mAlert.setMessage(message);
    
public voidsetTitle(java.lang.CharSequence title)

        super.setTitle(title);
        mAlert.setTitle(title);
    
public voidsetView(android.view.View view)
Set the view to display in that dialog.

        mAlert.setView(view);
    
public voidsetView(android.view.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.

param
view The view to show in the content area of the dialog
param
viewSpacingLeft Extra space to appear to the left of {@code view}
param
viewSpacingTop Extra space to appear above {@code view}
param
viewSpacingRight Extra space to appear to the right of {@code view}
param
viewSpacingBottom Extra space to appear below {@code view}

        mAlert.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);