AlertDialog
public class AlertDialog extends AppCompatDialog implements DialogInterface
| java.lang.Object | ||||
| ↳ | android.app.Dialog | |||
| ↳ | androidx.activity.ComponentDialog | |||
| ↳ | androidx.appcompat.app.AppCompatDialog | |||
| ↳ | androidx.appcompat.app.AlertDialog |
MediaRouteControllerDialog |
This class implements the route controller dialog for |
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 = findViewById(android.R.id.custom); fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
The AlertDialog class takes care of automatically setting android.view.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.
Summary
Nested types |
|---|
public class AlertDialog.Builder |
Protected constructors |
|---|
AlertDialog(@NonNull Context context) |
AlertDialog(@NonNull Context context, @StyleRes int themeResId)Construct an AlertDialog that uses an explicit theme. |
AlertDialog( |
Public methods |
|
|---|---|
Button |
getButton(int whichButton)Gets one of the buttons used in the dialog. |
ListView |
Gets the list view used in the dialog. |
boolean |
|
boolean |
|
void |
setButton(Sets a listener to be invoked when the positive button of the dialog is pressed. |
void |
setButton(int whichButton, CharSequence text, Message msg)Sets a message to be sent when a button is pressed. |
void |
setButton(Sets an icon to be displayed along with the button text and a listener to be invoked when the positive button of the dialog is pressed. |
void |
setCustomTitle(View customTitleView)This method has no effect if called after |
void |
Set the |
void |
setIcon(int resId)Set resId to 0 if you don't want an icon. |
void |
setIconAttribute(int attrId)Sets an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon |
void |
setMessage(CharSequence message)Sets the message to display. |
void |
setTitle(CharSequence title) |
void |
Set the view to display in the dialog. |
void |
setView(Set the view to display in the dialog, specifying the spacing to appear around that view. |
Inherited Constants |
||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Inherited methods |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Protected constructors
AlertDialog
protected AlertDialog(@NonNull Context context, @StyleRes int themeResId)
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.
AlertDialog
protected AlertDialog(
@NonNull Context context,
boolean cancelable,
@Nullable DialogInterface.OnCancelListener cancelListener
)
Public methods
getButton
public Button getButton(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 show or create).
| Parameters | |
|---|---|
int whichButton |
The identifier of the button that should be returned. For example, this can be |
| Returns | |
|---|---|
Button |
The button from the dialog, or null if a button does not exist. |
setButton
public void setButton(
int whichButton,
CharSequence text,
DialogInterface.OnClickListener listener
)
Sets a listener to be invoked when the positive button of the dialog is pressed. This method has no effect if called after show.
| Parameters | |
|---|---|
int whichButton |
Which button to set the listener on, can be one of |
CharSequence text |
The text to display in positive button. |
DialogInterface.OnClickListener listener |
The |
setButton
public void setButton(int whichButton, CharSequence text, Message msg)
Sets a message to be sent when a button is pressed. This method has no effect if called after show.
| Parameters | |
|---|---|
int whichButton |
Which button to set the message for, can be one of |
CharSequence text |
The text to display in positive button. |
Message msg |
The |
setButton
public void setButton(
int whichButton,
CharSequence text,
Drawable icon,
DialogInterface.OnClickListener listener
)
Sets an icon to be displayed along with the button text and a listener to be invoked when the positive button of the dialog is pressed. This method has no effect if called after show.
| Parameters | |
|---|---|
int whichButton |
Which button to set the listener on, can be one of |
CharSequence text |
The text to display in positive button. |
Drawable icon |
The |
DialogInterface.OnClickListener listener |
The |
setCustomTitle
public void setCustomTitle(View customTitleView)
This method has no effect if called after show.
| See also | |
|---|---|
setCustomTitle |
setIcon
public void setIcon(Drawable icon)
Set the Drawable to be used in the title.
| Parameters | |
|---|---|
Drawable icon |
Drawable to use as the icon or null if you don't want an icon. |
setIcon
public void setIcon(int resId)
Set resId to 0 if you don't want an icon.
| Parameters | |
|---|---|
int resId |
the resourceId of the drawable to use as the icon or 0 if you don't want an icon. |
setIconAttribute
public void setIconAttribute(int attrId)
Sets an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon
| Parameters | |
|---|---|
int attrId |
ID of a theme attribute that points to a drawable resource. |
setMessage
public void setMessage(CharSequence message)
Sets the message to display.
| Parameters | |
|---|---|
CharSequence message |
The message to display in the dialog. |
setView
public void setView(View view)
Set the view to display in the dialog. This method has no effect if called after show.
setView
public void setView(
View view,
int viewSpacingLeft,
int viewSpacingTop,
int viewSpacingRight,
int viewSpacingBottom
)
Set the view to display in the dialog, specifying the spacing to appear around that view. This method has no effect if called after show.
| Parameters | |
|---|---|
View view |
The view to show in the content area of the dialog |
int viewSpacingLeft |
Extra space to appear to the left of |
int viewSpacingTop |
Extra space to appear above |
int viewSpacingRight |
Extra space to appear to the right of |
int viewSpacingBottom |
Extra space to appear below |