FileDocCategorySizeDatePackage
AlertDialog.javaAPI DocAndroid 1.5 API34474Wed May 06 22:41:54 BST 2009android.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 "body" and add your view to it:
FrameLayout fl = (FrameLayout) findViewById(R.id.body);
fl.add(myView, new LayoutParams(FILL_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}.

Fields Summary
private com.android.internal.app.AlertController
mAlert
Constructors Summary
protected AlertDialog(android.content.Context context)

        this(context, com.android.internal.R.style.Theme_Dialog_Alert);
    
protected AlertDialog(android.content.Context context, int theme)

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

        super(context, com.android.internal.R.style.Theme_Dialog_Alert);
        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.

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

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);
    
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);
    
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 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, 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);
    
public voidsetView(android.view.View view)
Set the view to display in that dialog.

        mAlert.setView(view);