public class

AlertDialog.Builder

extends java.lang.Object

 java.lang.Object

↳androidx.appcompat.app.AlertDialog.Builder

Summary

Constructors
publicBuilder(Context context)

Creates a builder for an alert dialog that uses the default alert dialog theme.

publicBuilder(Context context, int themeResId)

Creates a builder for an alert dialog that uses an explicit theme resource.

Methods
public AlertDialogcreate()

Creates an AlertDialog with the arguments supplied to this builder.

public ContextgetContext()

Returns a with the appropriate theme for dialogs created by this Builder.

public AlertDialog.BuildersetAdapter(ListAdapter adapter, OnClickListener listener)

Set a list of items, which are supplied by the given , to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetCancelable(boolean cancelable)

Sets whether the dialog is cancelable or not.

public AlertDialog.BuildersetCursor(Cursor cursor, OnClickListener listener, java.lang.String labelColumn)

Set a list of items, which are supplied by the given , to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetCustomTitle(View customTitleView)

Set the title using the custom view customTitleView.

public AlertDialog.BuildersetIcon(Drawable icon)

Set the Drawable to be used in the title.

public AlertDialog.BuildersetIcon(int iconId)

Set the resource id of the Drawable to be used in the title.

public AlertDialog.BuildersetIconAttribute(int attrId)

Set an icon as supplied by a theme attribute.

public AlertDialog.BuildersetInverseBackgroundForced(boolean useInverseBackground)

Sets the Dialog to use the inverse background, regardless of what the contents is.

public AlertDialog.BuildersetItems(java.lang.CharSequence items[], OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetItems(int itemsId, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetMessage(java.lang.CharSequence message)

Set the message to display.

public AlertDialog.BuildersetMessage(int messageId)

Set the message to display using the given resource id.

public AlertDialog.BuildersetMultiChoiceItems(java.lang.CharSequence items[], boolean[] checkedItems[], OnMultiChoiceClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetMultiChoiceItems(Cursor cursor, java.lang.String isCheckedColumn, java.lang.String labelColumn, OnMultiChoiceClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetMultiChoiceItems(int itemsId, boolean[] checkedItems[], OnMultiChoiceClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetNegativeButton(java.lang.CharSequence text, OnClickListener listener)

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

public AlertDialog.BuildersetNegativeButton(int textId, OnClickListener listener)

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

public AlertDialog.BuildersetNegativeButtonIcon(Drawable icon)

Set an icon to be displayed for the negative button.

public AlertDialog.BuildersetNeutralButton(java.lang.CharSequence text, OnClickListener listener)

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

public AlertDialog.BuildersetNeutralButton(int textId, OnClickListener listener)

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

public AlertDialog.BuildersetNeutralButtonIcon(Drawable icon)

Set an icon to be displayed for the neutral button.

public AlertDialog.BuildersetOnCancelListener(OnCancelListener onCancelListener)

Sets the callback that will be called if the dialog is canceled.

public AlertDialog.BuildersetOnDismissListener(OnDismissListener onDismissListener)

Sets the callback that will be called when the dialog is dismissed for any reason.

public AlertDialog.BuildersetOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)

Sets a listener to be invoked when an item in the list is selected.

public AlertDialog.BuildersetOnKeyListener(OnKeyListener onKeyListener)

Sets the callback that will be called if a key is dispatched to the dialog.

public AlertDialog.BuildersetPositiveButton(java.lang.CharSequence text, OnClickListener listener)

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

public AlertDialog.BuildersetPositiveButton(int textId, OnClickListener listener)

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

public AlertDialog.BuildersetPositiveButtonIcon(Drawable icon)

Set an icon to be displayed for the positive button.

public AlertDialog.BuildersetRecycleOnMeasureEnabled(boolean enabled)

public AlertDialog.BuildersetSingleChoiceItems(java.lang.CharSequence items[], int checkedItem, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetSingleChoiceItems(Cursor cursor, int checkedItem, java.lang.String labelColumn, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetSingleChoiceItems(int itemsId, int checkedItem, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetSingleChoiceItems(ListAdapter adapter, int checkedItem, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

public AlertDialog.BuildersetTitle(java.lang.CharSequence title)

Set the title displayed in the Dialog.

public AlertDialog.BuildersetTitle(int titleId)

Set the title using the given resource id.

public AlertDialog.BuildersetView(int layoutResId)

Inflates a custom view resource inside the dialog, below the message and above the buttons.

public AlertDialog.BuildersetView(View view)

Sets a custom view to be the contents of the alert dialog.

public AlertDialog.BuildersetView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)

Set a custom view to be the contents of the Dialog, specifying the spacing to appear around that view.

public AlertDialogshow()

Creates an AlertDialog with the arguments supplied to this builder and immediately displays the dialog.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public Builder(Context context)

Creates a builder for an alert dialog that uses the default alert dialog theme.

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

Parameters:

context: the parent context

public Builder(Context context, int themeResId)

Creates a builder for 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 , 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 . 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 .

Parameters:

context: the parent context
themeResId: the resource ID of the theme against which to inflate this dialog, or 0 to use the parent context's default alert dialog theme

Methods

public Context getContext()

Returns a with the appropriate theme for dialogs created by this Builder. Applications should use this Context for obtaining LayoutInflaters for inflating views that will be used in the resulting dialogs, as it will cause views to be inflated with the correct theme.

Returns:

A Context for built Dialogs.

public AlertDialog.Builder setTitle(int titleId)

Set the title using the given resource id.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setTitle(java.lang.CharSequence title)

Set the title displayed in the Dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setCustomTitle(View customTitleView)

Set the title using the custom view customTitleView.

The methods AlertDialog.Builder.setTitle(int) and AlertDialog.Builder.setIcon(int) should be sufficient for most titles, but this is provided if the title needs more customization. Using this will replace the title and icon set via the other methods.

Note: To ensure consistent styling, the custom view should be inflated or constructed using the alert dialog's themed context obtained via AlertDialog.Builder.getContext().

Parameters:

customTitleView: the custom view to use as the title

Returns:

this Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setMessage(int messageId)

Set the message to display using the given resource id.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setMessage(java.lang.CharSequence message)

Set the message to display.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setIcon(int iconId)

Set the resource id of the Drawable to be used in the title.

Takes precedence over values set using AlertDialog.Builder.setIcon(Drawable).

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setIcon(Drawable icon)

Set the Drawable to be used in the title.

Note: To ensure consistent styling, the drawable should be inflated or constructed using the alert dialog's themed context obtained via AlertDialog.Builder.getContext().

Returns:

this Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setIconAttribute(int attrId)

Set an icon as supplied by a theme attribute. e.g. .

Takes precedence over values set using AlertDialog.Builder.setIcon(int) or AlertDialog.Builder.setIcon(Drawable).

Parameters:

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

public AlertDialog.Builder setPositiveButton(int textId, OnClickListener listener)

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

Parameters:

textId: The resource id of the text to display in the positive button
listener: The to use.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setPositiveButton(java.lang.CharSequence text, OnClickListener listener)

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

Parameters:

text: The text to display in the positive button
listener: The to use.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setPositiveButtonIcon(Drawable icon)

Set an icon to be displayed for the positive button.

Parameters:

icon: The icon to be displayed

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setNegativeButton(int textId, OnClickListener listener)

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

Parameters:

textId: The resource id of the text to display in the negative button
listener: The to use.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setNegativeButton(java.lang.CharSequence text, OnClickListener listener)

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

Parameters:

text: The text to display in the negative button
listener: The to use.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setNegativeButtonIcon(Drawable icon)

Set an icon to be displayed for the negative button.

Parameters:

icon: The icon to be displayed

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setNeutralButton(int textId, OnClickListener listener)

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

Parameters:

textId: The resource id of the text to display in the neutral button
listener: The to use.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setNeutralButton(java.lang.CharSequence text, OnClickListener listener)

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

Parameters:

text: The text to display in the neutral button
listener: The to use.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setNeutralButtonIcon(Drawable icon)

Set an icon to be displayed for the neutral button.

Parameters:

icon: The icon to be displayed

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setCancelable(boolean cancelable)

Sets whether the dialog is cancelable or not. Default is true.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setOnCancelListener(OnCancelListener onCancelListener)

Sets the callback that will be called if the dialog is canceled.

Even in a cancelable dialog, the dialog may be dismissed for reasons other than being canceled or one of the supplied choices being selected. If you are interested in listening for all cases where the dialog is dismissed and not just when it is canceled, see setOnDismissListener.

Returns:

This Builder object to allow for chaining of calls to set methods

See also: AlertDialog.Builder.setCancelable(boolean), AlertDialog.Builder

public AlertDialog.Builder setOnDismissListener(OnDismissListener onDismissListener)

Sets the callback that will be called when the dialog is dismissed for any reason.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setOnKeyListener(OnKeyListener onKeyListener)

Sets the callback that will be called if a key is dispatched to the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setItems(int itemsId, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. This should be an array type i.e. R.array.foo

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setItems(java.lang.CharSequence items[], OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setAdapter(ListAdapter adapter, OnClickListener listener)

Set a list of items, which are supplied by the given , to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

Parameters:

adapter: The to supply the list of items
listener: The listener that will be called when an item is clicked.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setCursor(Cursor cursor, OnClickListener listener, java.lang.String labelColumn)

Set a list of items, which are supplied by the given , to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

Parameters:

cursor: The to supply the list of items
listener: The listener that will be called when an item is clicked.
labelColumn: The column name on the cursor containing the string to display in the label.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems[], OnMultiChoiceClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. This should be an array type, e.g. R.array.foo. The list will have a check mark displayed to the right of the text for each checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

Parameters:

itemsId: the resource id of an array i.e. R.array.foo
checkedItems: specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
listener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setMultiChoiceItems(java.lang.CharSequence items[], boolean[] checkedItems[], OnMultiChoiceClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for each checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

Parameters:

items: the text of the items to be displayed in the list.
checkedItems: specifies which items are checked. It should be null in which case no items are checked. If non null it must be exactly the same length as the array of items.
listener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setMultiChoiceItems(Cursor cursor, java.lang.String isCheckedColumn, java.lang.String labelColumn, OnMultiChoiceClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for each checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

Parameters:

cursor: the cursor used to provide the items.
isCheckedColumn: specifies the column name on the cursor to use to determine whether a checkbox is checked or not. It must return an integer value where 1 means checked and 0 means unchecked.
labelColumn: The column name on the cursor containing the string to display in the label.
listener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setSingleChoiceItems(int itemsId, int checkedItem, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. This should be an array type i.e. R.array.foo The list will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

Parameters:

itemsId: the resource id of an array i.e. R.array.foo
checkedItem: specifies which item is checked. If -1 no items are checked.
listener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setSingleChoiceItems(Cursor cursor, int checkedItem, java.lang.String labelColumn, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

Parameters:

cursor: the cursor to retrieve the items from.
checkedItem: specifies which item is checked. If -1 no items are checked.
labelColumn: The column name on the cursor containing the string to display in the label.
listener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setSingleChoiceItems(java.lang.CharSequence items[], int checkedItem, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

Parameters:

items: the items to be displayed.
checkedItem: specifies which item is checked. If -1 no items are checked.
listener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, OnClickListener listener)

Set a list of items to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener. The list will have a check mark displayed to the right of the text for the checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a button will dismiss the dialog.

Parameters:

adapter: The to supply the list of items
checkedItem: specifies which item is checked. If -1 no items are checked.
listener: notified when an item on the list is clicked. The dialog will not be dismissed when an item is clicked. It will only be dismissed if clicked on a button, if no buttons are supplied it's up to the user to dismiss the dialog.

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)

Sets a listener to be invoked when an item in the list is selected.

Parameters:

listener: the listener to be invoked

Returns:

this Builder object to allow for chaining of calls to set methods

See also: AdapterView

public AlertDialog.Builder setView(int layoutResId)

Inflates a custom view resource inside the dialog, below the message and above the buttons. If none of the other parts of the dialog are made visible, the custom view occupies the entire view. Because the view is inflated inside the parent dialog's view, take care not to define conflicting resource IDs.

Parameters:

layoutResId: Resource ID to be inflated.

Returns:

this Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setView(View view)

Sets a custom view to be the contents of the alert dialog.

When using a pre-Holo theme, if the supplied view is an instance of a ListView then the light background will be used.

Note: To ensure consistent styling, the custom view should be inflated or constructed using the alert dialog's themed context obtained via AlertDialog.Builder.getContext().

Parameters:

view: the view to use as the contents of the alert dialog

Returns:

this Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)

Deprecated: This method has been deprecated.

Set a custom view to be the contents of the Dialog, specifying the spacing to appear around that view. If the supplied view is an instance of a ListView the light background will be used.

Parameters:

view: The view to use as the contents of the Dialog.
viewSpacingLeft: Spacing between the left edge of the view and the dialog frame
viewSpacingTop: Spacing between the top edge of the view and the dialog frame
viewSpacingRight: Spacing between the right edge of the view and the dialog frame
viewSpacingBottom: Spacing between the bottom edge of the view and the dialog frame

Returns:

This Builder object to allow for chaining of calls to set methods This is currently hidden because it seems like people should just be able to put padding around the view.

public AlertDialog.Builder setInverseBackgroundForced(boolean useInverseBackground)

Deprecated: This flag is only used for pre-Material themes. Instead, specify the window background using on the alert dialog theme.

Sets the Dialog to use the inverse background, regardless of what the contents is.

Parameters:

useInverseBackground: Whether to use the inverse background

Returns:

This Builder object to allow for chaining of calls to set methods

public AlertDialog.Builder setRecycleOnMeasureEnabled(boolean enabled)

public AlertDialog create()

Creates an AlertDialog with the arguments supplied to this builder.

Calling this method does not display the dialog. If no additional processing is needed, AlertDialog.Builder.show() may be called instead to both create and display the dialog.

public AlertDialog show()

Creates an AlertDialog with the arguments supplied to this builder and immediately displays the dialog.

Calling this method is functionally identical to:

     AlertDialog dialog = builder.create();
     dialog.show();