public class AlertDialog extends AppCompatDialog 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
mCancelable
BUTTON_NEGATIVE, BUTTON_NEUTRAL, BUTTON_POSITIVE, BUTTON1, BUTTON2, BUTTON3
Modifier | Constructor and Description |
---|---|
protected |
AlertDialog(Context context) |
protected |
AlertDialog(Context context,
boolean cancelable,
DialogInterface.OnCancelListener cancelListener) |
protected |
AlertDialog(Context context,
int themeResId)
Construct an AlertDialog that uses an explicit theme.
|
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(int whichButton,
CharSequence text,
DialogInterface.OnClickListener listener)
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 |
setCustomTitle(View customTitleView) |
void |
setIcon(Drawable icon)
Set the
Drawable to be used in the title. |
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)
Set the title text for this dialog's window.
|
void |
setView(View view)
Set the view to display in the dialog.
|
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.
|
addContentView, findViewById, getDelegate, getSupportActionBar, invalidateOptionsMenu, onStop, onSupportActionModeFinished, onSupportActionModeStarted, onWindowStartingSupportActionMode, setContentView, setContentView, setContentView, setTitle, supportRequestWindowFeature
cancel, closeOptionsMenu, create, dismiss, dispatchGenericMotionEvent, dispatchKeyEvent, dispatchKeyShortcutEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, getActionBar, getContext, getCurrentFocus, getLayoutInflater, getOwnerActivity, getSearchEvent, getVolumeControlStream, getWindow, hide, 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, onTouchEvent, onTrackballEvent, onWindowAttributesChanged, onWindowDismissed, onWindowFocusChanged, onWindowStartingActionMode, onWindowStartingActionMode, openContextMenu, openOptionsMenu, registerForContextMenu, requestWindowFeature, setCancelable, setCanceledOnTouchOutside, setCancelMessage, setDismissMessage, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setOnCancelListener, setOnDismissListener, setOnKeyListener, setOnShowListener, setOwnerActivity, setVolumeControlStream, show, takeCancelAndDismissListeners, takeKeyEvents, unregisterForContextMenu
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
cancel, dismiss
onProvideKeyboardShortcuts
protected AlertDialog(@NonNull Context context, @StyleRes int themeResId)
R.attr#alertDialogTheme
.protected AlertDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener)
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
setTitle
in class AppCompatDialog
title
- The new text to display in the title.public void setCustomTitle(View customTitleView)
public void setMessage(CharSequence message)
message
- The message to display in the dialog.public void setView(View view)
Dialog.show()
.public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom)
Dialog.show()
.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)
Dialog.show()
.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)
Dialog.show()
.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.public void setIcon(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)
Drawable
to be used in the title.icon
- Drawable to use as the icon or null if you don't want an icon.public void setIconAttribute(int attrId)
attrId
- ID of a theme attribute that points to a drawable resource.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 AppCompatDialog
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