public class AlertDialog extends Dialog implements DialogInterface
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
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(android.os.Bundle)
.
For more information about creating dialogs, read the Dialogs developer guide.
Modifier and Type | Class and Description |
---|---|
static class |
AlertDialog.Builder |
DialogInterface.OnCancelListener, DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnKeyListener, DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnShowListener
Modifier and Type | Field and Description |
---|---|
static int |
LAYOUT_HINT_NONE
No layout hint.
|
static int |
LAYOUT_HINT_SIDE
Hint layout to the side.
|
static int |
THEME_DEVICE_DEFAULT_DARK
Deprecated.
Use
android.R.style#Theme_DeviceDefault_Dialog_Alert . |
static int |
THEME_DEVICE_DEFAULT_LIGHT
Deprecated.
Use
android.R.style#Theme_DeviceDefault_Light_Dialog_Alert . |
static int |
THEME_HOLO_DARK
Deprecated.
Use
android.R.style#Theme_Material_Dialog_Alert . |
static int |
THEME_HOLO_LIGHT
Deprecated.
Use
android.R.style#Theme_Material_Light_Dialog_Alert . |
static int |
THEME_TRADITIONAL
Deprecated.
Use
android.R.style#Theme_Material_Dialog_Alert . |
mCancelable
BUTTON_NEGATIVE, BUTTON_NEUTRAL, BUTTON_POSITIVE, BUTTON1, BUTTON2, BUTTON3
Modifier | Constructor and Description |
---|---|
protected |
AlertDialog(Context context)
Creates an alert dialog that uses the default alert dialog theme.
|
protected |
AlertDialog(Context context,
boolean cancelable,
DialogInterface.OnCancelListener cancelListener)
Creates an alert dialog that uses the default alert dialog theme and a
custom cancel listener.
|
protected |
AlertDialog(Context context,
int themeResId)
Creates an alert dialog that uses an explicit theme resource.
|
Modifier and Type | Method and Description |
---|---|
Button |
getButton(int whichButton)
Gets one of the buttons used in the dialog.
|
ListView |
getListView()
Gets the list view used in the dialog.
|
protected void |
onCreate(Bundle savedInstanceState)
Similar to
Activity.onCreate(android.os.Bundle) , you should initialize your dialog
in this method, including calling Dialog.setContentView(int) . |
boolean |
onKeyDown(int keyCode,
KeyEvent event)
A key was pressed down.
|
boolean |
onKeyUp(int keyCode,
KeyEvent event)
A key was released.
|
void |
setButton(CharSequence text,
DialogInterface.OnClickListener listener)
|
void |
setButton(CharSequence text,
Message msg)
Deprecated.
|
void |
setButton(int whichButton,
CharSequence text,
DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
|
void |
setButton(int whichButton,
CharSequence text,
Message msg)
Set a message to be sent when a button is pressed.
|
void |
setButton2(CharSequence text,
DialogInterface.OnClickListener listener)
|
void |
setButton2(CharSequence text,
Message msg)
Deprecated.
|
void |
setButton3(CharSequence text,
DialogInterface.OnClickListener listener)
|
void |
setButton3(CharSequence text,
Message msg)
Deprecated.
|
void |
setCustomTitle(View customTitleView) |
void |
setIcon(Drawable icon) |
void |
setIcon(int resId)
Set resId to 0 if you don't want an icon.
|
void |
setIconAttribute(int attrId)
Set an icon as supplied by a theme attribute. e.g. android.R.attr.alertDialogIcon
|
void |
setInverseBackgroundForced(boolean forceInverseBackground) |
void |
setMessage(CharSequence message) |
void |
setTitle(CharSequence title)
Set the title text for this dialog's window.
|
void |
setView(View view)
Set the view to display in that dialog.
|
void |
setView(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.
|
addContentView, cancel, closeOptionsMenu, create, dismiss, dispatchGenericMotionEvent, dispatchKeyEvent, dispatchKeyShortcutEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, findViewById, getActionBar, getContext, getCurrentFocus, getLayoutInflater, getOwnerActivity, getSearchEvent, getVolumeControlStream, getWindow, hide, invalidateOptionsMenu, isShowing, onActionModeFinished, onActionModeStarted, onAttachedToWindow, onBackPressed, onContentChanged, onContextItemSelected, onContextMenuClosed, onCreateContextMenu, onCreateOptionsMenu, onCreatePanelMenu, onCreatePanelView, onDetachedFromWindow, onGenericMotionEvent, onKeyLongPress, onKeyMultiple, onKeyShortcut, onMenuItemSelected, onMenuOpened, onOptionsItemSelected, onOptionsMenuClosed, onPanelClosed, onPrepareOptionsMenu, onPreparePanel, onRestoreInstanceState, onSaveInstanceState, onSearchRequested, onSearchRequested, onStart, onStop, onTouchEvent, onTrackballEvent, onWindowAttributesChanged, onWindowDismissed, onWindowFocusChanged, onWindowStartingActionMode, onWindowStartingActionMode, openContextMenu, openOptionsMenu, registerForContextMenu, requestWindowFeature, setCancelable, setCanceledOnTouchOutside, setCancelMessage, setContentView, setContentView, setContentView, setDismissMessage, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setOnCancelListener, setOnDismissListener, setOnKeyListener, setOnShowListener, setOwnerActivity, setTitle, setVolumeControlStream, show, takeCancelAndDismissListeners, takeKeyEvents, unregisterForContextMenu
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
cancel, dismiss
onProvideKeyboardShortcuts
@Deprecated public static final int THEME_TRADITIONAL
android.R.style#Theme_Material_Dialog_Alert
.AlertDialog(Context, int)
: use
the traditional (pre-Holo) alert dialog theme.@Deprecated public static final int THEME_HOLO_DARK
android.R.style#Theme_Material_Dialog_Alert
.AlertDialog(Context, int)
: use
the holographic alert theme with a dark background.@Deprecated public static final int THEME_HOLO_LIGHT
android.R.style#Theme_Material_Light_Dialog_Alert
.AlertDialog(Context, int)
: use
the holographic alert theme with a light background.@Deprecated public static final int THEME_DEVICE_DEFAULT_DARK
android.R.style#Theme_DeviceDefault_Dialog_Alert
.AlertDialog(Context, int)
: use
the device's default alert theme with a dark background.@Deprecated public static final int THEME_DEVICE_DEFAULT_LIGHT
android.R.style#Theme_DeviceDefault_Light_Dialog_Alert
.AlertDialog(Context, int)
: use
the device's default alert theme with a light background.public static final int LAYOUT_HINT_NONE
public static final int LAYOUT_HINT_SIDE
protected AlertDialog(Context context)
The default alert dialog theme is defined by
android.R.attr#alertDialogTheme
within the parent
context
's theme.
context
- the parent contextandroid.R.styleable#Theme_alertDialogTheme
protected AlertDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)
This is functionally identical to:
AlertDialog dialog = new AlertDialog(context); alertDialog.setCancelable(cancelable); alertDialog.setOnCancelListener(cancelListener);
The default alert dialog theme is defined by
android.R.attr#alertDialogTheme
within the parent
context
's theme.
context
- the parent contextandroid.R.styleable#Theme_alertDialogTheme
protected AlertDialog(Context context, @StyleRes 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 themeandroid.R.styleable#Theme_alertDialogTheme
public Button getButton(int whichButton)
Dialog.show()
or Dialog.create()
).whichButton
- The identifier of the button that should be returned.
For example, this can be
DialogInterface.BUTTON_POSITIVE
.public ListView getListView()
ListView
from the dialog.public void setTitle(CharSequence title)
Dialog
public void setCustomTitle(View customTitleView)
AlertDialog.Builder.setCustomTitle(View)
public void setMessage(CharSequence message)
public void setView(View view)
public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)
view
- The view to show in the content area of the dialogviewSpacingLeft
- Extra space to appear to the left of view
viewSpacingTop
- Extra space to appear above view
viewSpacingRight
- Extra space to appear to the right of view
viewSpacingBottom
- Extra space to appear below view
public void setButton(int whichButton, CharSequence text, Message msg)
whichButton
- Which button to set the message for, can be one of
DialogInterface.BUTTON_POSITIVE
,
DialogInterface.BUTTON_NEGATIVE
, or
DialogInterface.BUTTON_NEUTRAL
text
- The text to display in positive button.msg
- The Message
to be sent when clicked.public void setButton(int whichButton, CharSequence text, DialogInterface.OnClickListener listener)
whichButton
- Which button to set the listener on, can be one of
DialogInterface.BUTTON_POSITIVE
,
DialogInterface.BUTTON_NEGATIVE
, or
DialogInterface.BUTTON_NEUTRAL
text
- The text to display in positive button.listener
- The DialogInterface.OnClickListener
to use.@Deprecated public void setButton(CharSequence text, Message msg)
setButton(int, CharSequence, Message)
with
DialogInterface.BUTTON_POSITIVE
.@Deprecated public void setButton2(CharSequence text, Message msg)
setButton(int, CharSequence, Message)
with
DialogInterface.BUTTON_NEGATIVE
.@Deprecated public void setButton3(CharSequence text, Message msg)
setButton(int, CharSequence, Message)
with
DialogInterface.BUTTON_NEUTRAL
.@Deprecated public void setButton(CharSequence text, DialogInterface.OnClickListener listener)
setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)
with DialogInterface.BUTTON_POSITIVE
text
- The text to display in button 1.listener
- The DialogInterface.OnClickListener
to use.@Deprecated public void setButton2(CharSequence text, DialogInterface.OnClickListener listener)
setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)
with DialogInterface.BUTTON_NEGATIVE
text
- The text to display in button 2.listener
- The DialogInterface.OnClickListener
to use.@Deprecated public void setButton3(CharSequence text, DialogInterface.OnClickListener listener)
setButton(int, CharSequence, android.content.DialogInterface.OnClickListener)
with DialogInterface.BUTTON_POSITIVE
text
- The text to display in button 3.listener
- The DialogInterface.OnClickListener
to use.public void setIcon(@DrawableRes int resId)
resId
- the resourceId of the drawable to use as the icon or 0
if you don't want an icon.public void setIcon(Drawable icon)
public void setIconAttribute(@AttrRes int attrId)
attrId
- ID of a theme attribute that points to a drawable resource.public void setInverseBackgroundForced(boolean forceInverseBackground)
protected void onCreate(Bundle savedInstanceState)
Dialog
Activity.onCreate(android.os.Bundle)
, you should initialize your dialog
in this method, including calling Dialog.setContentView(int)
.onCreate
in class Dialog
savedInstanceState
- If this dialog is being reinitialized after a
the hosting activity was previously shut down, holds the result from
the most recent call to Dialog.onSaveInstanceState()
, or null if this
is the first time.public boolean onKeyDown(int keyCode, KeyEvent event)
Dialog
If the focused view didn't want this event, this method is called.
The default implementation consumed the KEYCODE_BACK to later
handle it in Dialog.onKeyUp(int, android.view.KeyEvent)
.
onKeyDown
in interface KeyEvent.Callback
onKeyDown
in class Dialog
keyCode
- The value in event.getKeyCode().event
- Description of the key event.Dialog.onKeyUp(int, android.view.KeyEvent)
,
KeyEvent
public boolean onKeyUp(int keyCode, KeyEvent event)
Dialog
The default implementation handles KEYCODE_BACK to close the dialog.
onKeyUp
in interface KeyEvent.Callback
onKeyUp
in class Dialog
keyCode
- The value in event.getKeyCode().event
- Description of the key event.Dialog.onKeyDown(int, android.view.KeyEvent)
,
KeyEvent