public static class AlertDialog.Builder extends Object
Constructor and Description |
---|
Builder(Context context)
Creates a builder for an alert dialog that uses the default alert
dialog theme.
|
Builder(Context context,
int themeResId)
Creates a builder for an alert dialog that uses an explicit theme
resource.
|
Modifier and Type | Method and Description |
---|---|
AlertDialog |
create()
Creates an
AlertDialog with the arguments supplied to this
builder. |
Context |
getContext()
Returns a
Context with the appropriate theme for dialogs created by this Builder. |
AlertDialog.Builder |
setAdapter(ListAdapter adapter,
DialogInterface.OnClickListener listener)
Set a list of items, which are supplied by the given
ListAdapter , to be
displayed in the dialog as the content, you will be notified of the
selected item via the supplied listener. |
AlertDialog.Builder |
setCancelable(boolean cancelable)
Sets whether the dialog is cancelable or not.
|
AlertDialog.Builder |
setCursor(Cursor cursor,
DialogInterface.OnClickListener listener,
String labelColumn)
Set a list of items, which are supplied by the given
Cursor , to be
displayed in the dialog as the content, you will be notified of the
selected item via the supplied listener. |
AlertDialog.Builder |
setCustomTitle(View customTitleView)
Set the title using the custom view
customTitleView . |
AlertDialog.Builder |
setIcon(Drawable icon)
Set the
Drawable to be used in the title. |
AlertDialog.Builder |
setIcon(int iconId)
Set the resource id of the
Drawable to be used in the title. |
AlertDialog.Builder |
setIconAttribute(int attrId)
Set an icon as supplied by a theme attribute. e.g.
|
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.
|
AlertDialog.Builder |
setItems(CharSequence[] items,
DialogInterface.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.
|
AlertDialog.Builder |
setItems(int itemsId,
DialogInterface.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.
|
AlertDialog.Builder |
setMessage(CharSequence message)
Set the message to display.
|
AlertDialog.Builder |
setMessage(int messageId)
Set the message to display using the given resource id.
|
AlertDialog.Builder |
setMultiChoiceItems(CharSequence[] items,
boolean[] checkedItems,
DialogInterface.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.
|
AlertDialog.Builder |
setMultiChoiceItems(Cursor cursor,
String isCheckedColumn,
String labelColumn,
DialogInterface.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.
|
AlertDialog.Builder |
setMultiChoiceItems(int itemsId,
boolean[] checkedItems,
DialogInterface.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.
|
AlertDialog.Builder |
setNegativeButton(CharSequence text,
DialogInterface.OnClickListener listener)
Set a listener to be invoked when the negative button of the dialog is pressed.
|
AlertDialog.Builder |
setNegativeButton(int textId,
DialogInterface.OnClickListener listener)
Set a listener to be invoked when the negative button of the dialog is pressed.
|
AlertDialog.Builder |
setNeutralButton(CharSequence text,
DialogInterface.OnClickListener listener)
Set a listener to be invoked when the neutral button of the dialog is pressed.
|
AlertDialog.Builder |
setNeutralButton(int textId,
DialogInterface.OnClickListener listener)
Set a listener to be invoked when the neutral button of the dialog is pressed.
|
AlertDialog.Builder |
setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)
Sets the callback that will be called if the dialog is canceled.
|
AlertDialog.Builder |
setOnDismissListener(DialogInterface.OnDismissListener onDismissListener)
Sets the callback that will be called when the dialog is dismissed for any reason.
|
AlertDialog.Builder |
setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)
Sets a listener to be invoked when an item in the list is selected.
|
AlertDialog.Builder |
setOnKeyListener(DialogInterface.OnKeyListener onKeyListener)
Sets the callback that will be called if a key is dispatched to the dialog.
|
AlertDialog.Builder |
setPositiveButton(CharSequence text,
DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
|
AlertDialog.Builder |
setPositiveButton(int textId,
DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
|
AlertDialog.Builder |
setRecycleOnMeasureEnabled(boolean enabled) |
AlertDialog.Builder |
setSingleChoiceItems(CharSequence[] items,
int checkedItem,
DialogInterface.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.
|
AlertDialog.Builder |
setSingleChoiceItems(Cursor cursor,
int checkedItem,
String labelColumn,
DialogInterface.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.
|
AlertDialog.Builder |
setSingleChoiceItems(int itemsId,
int checkedItem,
DialogInterface.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.
|
AlertDialog.Builder |
setSingleChoiceItems(ListAdapter adapter,
int checkedItem,
DialogInterface.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.
|
AlertDialog.Builder |
setTitle(CharSequence title)
Set the title displayed in the
Dialog . |
AlertDialog.Builder |
setTitle(int titleId)
Set the title using the given resource id.
|
AlertDialog.Builder |
setView(int layoutResId)
Set a custom view resource to be the contents of the Dialog.
|
AlertDialog.Builder |
setView(View view)
Sets a custom view to be the contents of the alert dialog.
|
AlertDialog.Builder |
setView(View view,
int viewSpacingLeft,
int viewSpacingTop,
int viewSpacingRight,
int viewSpacingBottom)
Deprecated.
Set the padding on the view itself.
|
AlertDialog |
show()
Creates an
AlertDialog with the arguments supplied to this
builder and immediately displays the dialog. |
public Builder(Context context)
The default alert dialog theme is defined by
android.R.attr#alertDialogTheme
within the parent
context
's theme.
context
- the parent contextpublic Builder(Context context, int themeResId)
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
android.R.style#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 android.R.style#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
android.R.attr#alertDialogTheme
.
context
- the parent contextthemeResId
- the resource ID of the theme against which to inflate
this dialog, or 0
to use the parent
context
's default alert dialog themepublic Context getContext()
Context
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.public AlertDialog.Builder setTitle(@StringRes int titleId)
public AlertDialog.Builder setTitle(CharSequence title)
Dialog
.public AlertDialog.Builder setCustomTitle(View customTitleView)
customTitleView
.
The methods setTitle(int)
and 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 getContext()
.
customTitleView
- the custom view to use as the titlepublic AlertDialog.Builder setMessage(@StringRes int messageId)
public AlertDialog.Builder setMessage(CharSequence message)
public AlertDialog.Builder setIcon(@DrawableRes int iconId)
Drawable
to be used in the title.
Takes precedence over values set using setIcon(Drawable)
.
public AlertDialog.Builder setIcon(Drawable icon)
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 getContext()
.
public AlertDialog.Builder setIconAttribute(@AttrRes int attrId)
android.R.attr#alertDialogIcon
.
Takes precedence over values set using setIcon(int)
or
setIcon(Drawable)
.
attrId
- ID of a theme attribute that points to a drawable resource.public AlertDialog.Builder setPositiveButton(@StringRes int textId, DialogInterface.OnClickListener listener)
textId
- The resource id of the text to display in the positive buttonlistener
- The DialogInterface.OnClickListener
to use.public AlertDialog.Builder setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
text
- The text to display in the positive buttonlistener
- The DialogInterface.OnClickListener
to use.public AlertDialog.Builder setNegativeButton(@StringRes int textId, DialogInterface.OnClickListener listener)
textId
- The resource id of the text to display in the negative buttonlistener
- The DialogInterface.OnClickListener
to use.public AlertDialog.Builder setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)
text
- The text to display in the negative buttonlistener
- The DialogInterface.OnClickListener
to use.public AlertDialog.Builder setNeutralButton(@StringRes int textId, DialogInterface.OnClickListener listener)
textId
- The resource id of the text to display in the neutral buttonlistener
- The DialogInterface.OnClickListener
to use.public AlertDialog.Builder setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener)
text
- The text to display in the neutral buttonlistener
- The DialogInterface.OnClickListener
to use.public AlertDialog.Builder setCancelable(boolean cancelable)
public AlertDialog.Builder setOnCancelListener(DialogInterface.OnCancelListener onCancelListener)
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
.
setCancelable(boolean)
,
setOnDismissListener(android.content.DialogInterface.OnDismissListener)
public AlertDialog.Builder setOnDismissListener(DialogInterface.OnDismissListener onDismissListener)
public AlertDialog.Builder setOnKeyListener(DialogInterface.OnKeyListener onKeyListener)
public AlertDialog.Builder setItems(@ArrayRes int itemsId, DialogInterface.OnClickListener listener)
public AlertDialog.Builder setItems(CharSequence[] items, DialogInterface.OnClickListener listener)
public AlertDialog.Builder setAdapter(ListAdapter adapter, DialogInterface.OnClickListener listener)
ListAdapter
, to be
displayed in the dialog as the content, you will be notified of the
selected item via the supplied listener.adapter
- The ListAdapter
to supply the list of itemslistener
- The listener that will be called when an item is clicked.public AlertDialog.Builder setCursor(Cursor cursor, DialogInterface.OnClickListener listener, String labelColumn)
Cursor
, to be
displayed in the dialog as the content, you will be notified of the
selected item via the supplied listener.cursor
- The Cursor
to supply the list of itemslistener
- 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.public AlertDialog.Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
itemsId
- the resource id of an array i.e. R.array.foocheckedItems
- 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.public AlertDialog.Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
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.public AlertDialog.Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, DialogInterface.OnMultiChoiceClickListener listener)
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.public AlertDialog.Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem, DialogInterface.OnClickListener listener)
itemsId
- the resource id of an array i.e. R.array.foocheckedItem
- 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.public AlertDialog.Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, DialogInterface.OnClickListener listener)
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.public AlertDialog.Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)
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.public AlertDialog.Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)
adapter
- The ListAdapter
to supply the list of itemscheckedItem
- 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.public AlertDialog.Builder setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)
listener
- the listener to be invokedAdapterView.setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
public AlertDialog.Builder setView(int layoutResId)
layoutResId
- Resource ID to be inflated.public AlertDialog.Builder setView(View view)
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 getContext()
.
view
- the view to use as the contents of the alert dialog@Deprecated public AlertDialog.Builder setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)
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 getContext()
.
view
- the view to use as the contents of the alert dialogviewSpacingLeft
- spacing between the left edge of the view and
the dialog frameviewSpacingTop
- spacing between the top edge of the view and
the dialog frameviewSpacingRight
- spacing between the right edge of the view
and the dialog frameviewSpacingBottom
- spacing between the bottom edge of the view
and the dialog frame@Deprecated public AlertDialog.Builder setInverseBackgroundForced(boolean useInverseBackground)
useInverseBackground
- whether to use the inverse backgroundpublic AlertDialog.Builder setRecycleOnMeasureEnabled(boolean enabled)
public AlertDialog create()
AlertDialog
with the arguments supplied to this
builder.
Calling this method does not display the dialog. If no additional
processing is needed, show()
may be called instead to both
create and display the dialog.
public AlertDialog show()
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();