public class Dialog extends Object implements DialogInterface, Window.Callback, KeyEvent.Callback, View.OnCreateContextMenuListener
Note: Activities provide a facility to manage the creation, saving and
restoring of dialogs. See Activity.onCreateDialog(int)
,
Activity.onPrepareDialog(int, Dialog)
,
Activity.showDialog(int)
, and Activity.dismissDialog(int)
. If
these methods are used, getOwnerActivity()
will return the Activity
that managed this dialog.
Often you will want to have a Dialog display on top of the current
input method, because there is no reason for it to accept text. You can
do this by setting the WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
window flag (assuming
your Dialog takes input focus, as it the default) with the following code:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
For more information about creating dialogs, read the Dialogs developer guide.
DialogInterface.OnCancelListener, DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnKeyListener, DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnShowListener
Modifier and Type | Field and Description |
---|---|
protected boolean |
mCancelable
This field should be made private, so it is hidden from the SDK.
|
BUTTON_NEGATIVE, BUTTON_NEUTRAL, BUTTON_POSITIVE, BUTTON1, BUTTON2, BUTTON3
Modifier | Constructor and Description |
---|---|
|
Dialog(Context context)
Create a Dialog window that uses the default dialog frame style.
|
protected |
Dialog(Context context,
boolean cancelable,
DialogInterface.OnCancelListener cancelListener) |
protected |
Dialog(Context context,
boolean cancelable,
Message cancelCallback)
Deprecated.
|
|
Dialog(Context context,
int theme)
Create a Dialog window that uses a custom dialog style.
|
Modifier and Type | Method and Description |
---|---|
void |
addContentView(View view,
ViewGroup.LayoutParams params)
Add an additional content view to the screen.
|
void |
cancel()
Cancel the dialog.
|
void |
closeOptionsMenu() |
void |
dismiss()
Dismiss this dialog, removing it from the screen.
|
boolean |
dispatchGenericMotionEvent(MotionEvent ev)
Called to process generic motion events.
|
boolean |
dispatchKeyEvent(KeyEvent event)
Called to process key events.
|
boolean |
dispatchKeyShortcutEvent(KeyEvent event)
Called to process a key shortcut event.
|
boolean |
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)
Called to process population of
AccessibilityEvent s. |
boolean |
dispatchTouchEvent(MotionEvent ev)
Called to process touch screen events.
|
boolean |
dispatchTrackballEvent(MotionEvent ev)
Called to process trackball events.
|
View |
findViewById(int id)
Finds a view that was identified by the id attribute from the XML that
was processed in
onStart() . |
ActionBar |
getActionBar()
Retrieve the
ActionBar attached to this dialog, if present. |
Context |
getContext()
Retrieve the Context this Dialog is running in.
|
View |
getCurrentFocus()
Call
Window.getCurrentFocus() on the
Window if this Activity to return the currently focused view. |
LayoutInflater |
getLayoutInflater() |
Activity |
getOwnerActivity()
Returns the Activity that owns this Dialog.
|
int |
getVolumeControlStream() |
Window |
getWindow()
Retrieve the current Window for the activity.
|
void |
hide()
Hide the dialog, but do not dismiss it.
|
void |
invalidateOptionsMenu() |
boolean |
isShowing() |
void |
onActionModeFinished(ActionMode mode)
Called when an action mode has been finished.
|
void |
onActionModeStarted(ActionMode mode)
Called when an action mode has been started.
|
void |
onAttachedToWindow()
Called when the window has been attached to the window manager.
|
void |
onBackPressed()
Called when the dialog has detected the user's press of the back
key.
|
void |
onContentChanged()
This hook is called whenever the content view of the screen changes
(due to a call to
Window.setContentView or
Window.addContentView ). |
boolean |
onContextItemSelected(MenuItem item) |
void |
onContextMenuClosed(Menu menu) |
protected void |
onCreate(Bundle savedInstanceState)
Similar to
Activity.onCreate(android.os.Bundle) , you should initialize your dialog
in this method, including calling setContentView(int) . |
void |
onCreateContextMenu(ContextMenu menu,
View v,
ContextMenu.ContextMenuInfo menuInfo)
Called when the context menu for this view is being built.
|
boolean |
onCreateOptionsMenu(Menu menu)
It is usually safe to proxy this call to the owner activity's
Activity.onCreateOptionsMenu(Menu) if the client desires the same
menu for this Dialog. |
boolean |
onCreatePanelMenu(int featureId,
Menu menu)
Initialize the contents of the menu for panel 'featureId'.
|
View |
onCreatePanelView(int featureId)
Instantiate the view to display in the panel for 'featureId'.
|
void |
onDetachedFromWindow()
Called when the window has been attached to the window manager.
|
boolean |
onGenericMotionEvent(MotionEvent event)
Called when a generic motion event was not handled by any of the
views inside of the dialog.
|
boolean |
onKeyDown(int keyCode,
KeyEvent event)
A key was pressed down.
|
boolean |
onKeyLongPress(int keyCode,
KeyEvent event)
Default implementation of
KeyEvent.Callback.onKeyLongPress() : always returns false (doesn't handle
the event). |
boolean |
onKeyMultiple(int keyCode,
int repeatCount,
KeyEvent event)
Default implementation of
KeyEvent.Callback.onKeyMultiple() : always returns false (doesn't handle
the event). |
boolean |
onKeyShortcut(int keyCode,
KeyEvent event)
Called when a key shortcut event is not handled by any of the views in the Dialog.
|
boolean |
onKeyUp(int keyCode,
KeyEvent event)
A key was released.
|
boolean |
onMenuItemSelected(int featureId,
MenuItem item)
Called when a panel's menu item has been selected by the user.
|
boolean |
onMenuOpened(int featureId,
Menu menu)
Called when a panel's menu is opened by the user.
|
boolean |
onOptionsItemSelected(MenuItem item) |
void |
onOptionsMenuClosed(Menu menu) |
void |
onPanelClosed(int featureId,
Menu menu)
Called when a panel is being closed.
|
boolean |
onPrepareOptionsMenu(Menu menu)
It is usually safe to proxy this call to the owner activity's
Activity.onPrepareOptionsMenu(Menu) if the client desires the
same menu for this Dialog. |
boolean |
onPreparePanel(int featureId,
View view,
Menu menu)
Prepare a panel to be displayed.
|
void |
onRestoreInstanceState(Bundle savedInstanceState)
Restore the state of the dialog from a previously saved bundle.
|
Bundle |
onSaveInstanceState()
Saves the state of the dialog into a bundle.
|
boolean |
onSearchRequested()
This hook is called when the user signals the desire to start a search.
|
protected void |
onStart()
Called when the dialog is starting.
|
protected void |
onStop()
Called to tell you that you're stopping.
|
boolean |
onTouchEvent(MotionEvent event)
Called when a touch screen event was not handled by any of the views
under it.
|
boolean |
onTrackballEvent(MotionEvent event)
Called when the trackball was moved and not handled by any of the
views inside of the activity.
|
void |
onWindowAttributesChanged(WindowManager.LayoutParams params)
This is called whenever the current window attributes change.
|
void |
onWindowFocusChanged(boolean hasFocus)
This hook is called whenever the window focus changes.
|
ActionMode |
onWindowStartingActionMode(ActionMode.Callback callback)
Called when an action mode is being started for this window.
|
void |
openContextMenu(View view) |
void |
openOptionsMenu() |
void |
registerForContextMenu(View view) |
boolean |
requestWindowFeature(int featureId)
Enable extended window features.
|
void |
setCancelable(boolean flag)
Sets whether this dialog is cancelable with the
BACK key. |
void |
setCanceledOnTouchOutside(boolean cancel)
Sets whether this dialog is canceled when touched outside the window's
bounds.
|
void |
setCancelMessage(Message msg)
Set a message to be sent when the dialog is canceled.
|
void |
setContentView(int layoutResID)
Set the screen content from a layout resource.
|
void |
setContentView(View view)
Set the screen content to an explicit view.
|
void |
setContentView(View view,
ViewGroup.LayoutParams params)
Set the screen content to an explicit view.
|
void |
setDismissMessage(Message msg)
Set a message to be sent when the dialog is dismissed.
|
void |
setFeatureDrawable(int featureId,
Drawable drawable)
Convenience for calling
Window.setFeatureDrawable(int, Drawable) . |
void |
setFeatureDrawableAlpha(int featureId,
int alpha)
Convenience for calling
Window.setFeatureDrawableAlpha(int, int) . |
void |
setFeatureDrawableResource(int featureId,
int resId)
Convenience for calling
Window.setFeatureDrawableResource(int, int) . |
void |
setFeatureDrawableUri(int featureId,
Uri uri)
Convenience for calling
Window.setFeatureDrawableUri(int, android.net.Uri) . |
void |
setOnCancelListener(DialogInterface.OnCancelListener listener)
Set a listener to be invoked when the dialog is canceled.
|
void |
setOnDismissListener(DialogInterface.OnDismissListener listener)
Set a listener to be invoked when the dialog is dismissed.
|
void |
setOnKeyListener(DialogInterface.OnKeyListener onKeyListener)
Sets the callback that will be called if a key is dispatched to the dialog.
|
void |
setOnShowListener(DialogInterface.OnShowListener listener)
Sets a listener to be invoked when the dialog is shown.
|
void |
setOwnerActivity(Activity activity)
Sets the Activity that owns this dialog.
|
void |
setTitle(CharSequence title)
Set the title text for this dialog's window.
|
void |
setTitle(int titleId)
Set the title text for this dialog's window.
|
void |
setVolumeControlStream(int streamType)
By default, this will use the owner Activity's suggested stream type.
|
void |
show()
Start the dialog and display it on screen.
|
boolean |
takeCancelAndDismissListeners(String msg,
DialogInterface.OnCancelListener cancel,
DialogInterface.OnDismissListener dismiss) |
void |
takeKeyEvents(boolean get)
Request that key events come to this dialog.
|
void |
unregisterForContextMenu(View view) |
protected boolean mCancelable
public Dialog(Context context)
context
- The Context the Dialog is to run it. In particular, it
uses the window manager and theme in this context to
present its UI.public Dialog(Context context, int theme)
context
- The Context in which the Dialog should run. In particular, it
uses the window manager and theme from this context to
present its UI.theme
- A style resource describing the theme to use for the
window. See Style
and Theme Resources for more information about defining and using
styles. This theme is applied on top of the current theme in
context. If 0, the default dialog theme will be used.@Deprecated protected Dialog(Context context, boolean cancelable, Message cancelCallback)
protected Dialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)
public final Context getContext()
public ActionBar getActionBar()
ActionBar
attached to this dialog, if present.public final void setOwnerActivity(Activity activity)
activity
- The Activity that owns this dialog.public final Activity getOwnerActivity()
Activity.showDialog(int)
is used to show this Dialog, that
Activity will be the owner (by default). Depending on how this dialog was
created, this may return null.public boolean isShowing()
public void show()
onStart()
.public void hide()
public void dismiss()
onStop()
.dismiss
in interface DialogInterface
protected void onCreate(Bundle savedInstanceState)
Activity.onCreate(android.os.Bundle)
, you should initialize your dialog
in this method, including calling setContentView(int)
.savedInstanceState
- If this dialog is being reinitalized after a
the hosting activity was previously shut down, holds the result from
the most recent call to onSaveInstanceState()
, or null if this
is the first time.protected void onStart()
protected void onStop()
public Bundle onSaveInstanceState()
public void onRestoreInstanceState(Bundle savedInstanceState)
onSaveInstanceState()
,
so be sure to call through to super when overriding unless you want to
do all restoring of state yourself.savedInstanceState
- The state of the dialog previously saved by
onSaveInstanceState()
.public Window getWindow()
public View getCurrentFocus()
Window.getCurrentFocus()
on the
Window if this Activity to return the currently focused view.getWindow()
,
Window.getCurrentFocus()
public View findViewById(int id)
onStart()
.id
- the identifier of the view to findpublic void setContentView(int layoutResID)
layoutResID
- Resource ID to be inflated.public void setContentView(View view)
view
- The desired content to display.public void setContentView(View view, ViewGroup.LayoutParams params)
view
- The desired content to display.params
- Layout parameters for the view.public void addContentView(View view, ViewGroup.LayoutParams params)
view
- The desired content to display.params
- Layout parameters for the view.public void setTitle(CharSequence title)
title
- The new text to display in the title.public void setTitle(int titleId)
titleId
- the title's text resource identifierpublic boolean onKeyDown(int keyCode, KeyEvent event)
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 onKeyUp(int, android.view.KeyEvent)
.
onKeyDown
in interface KeyEvent.Callback
keyCode
- The value in event.getKeyCode().event
- Description of the key event.onKeyUp(int, android.view.KeyEvent)
,
KeyEvent
public boolean onKeyLongPress(int keyCode, KeyEvent event)
KeyEvent.Callback.onKeyLongPress()
: always returns false (doesn't handle
the event).onKeyLongPress
in interface KeyEvent.Callback
keyCode
- The value in event.getKeyCode().event
- Description of the key event.public boolean onKeyUp(int keyCode, KeyEvent event)
The default implementation handles KEYCODE_BACK to close the dialog.
onKeyUp
in interface KeyEvent.Callback
keyCode
- The value in event.getKeyCode().event
- Description of the key event.onKeyDown(int, android.view.KeyEvent)
,
KeyEvent
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
KeyEvent.Callback.onKeyMultiple()
: always returns false (doesn't handle
the event).onKeyMultiple
in interface KeyEvent.Callback
keyCode
- The value in event.getKeyCode().repeatCount
- Number of pairs as returned by event.getRepeatCount().event
- Description of the key event.public void onBackPressed()
public boolean onKeyShortcut(int keyCode, KeyEvent event)
shortcut
property of menu items.keyCode
- The value in event.getKeyCode().event
- Description of the key event.public boolean onTouchEvent(MotionEvent event)
event
- The touch screen event being processed.public boolean onTrackballEvent(MotionEvent event)
event
- The trackball event being processed.public boolean onGenericMotionEvent(MotionEvent event)
Generic motion events describe joystick movements, mouse hovers, track pad
touches, scroll wheel movements and other input events. The
source
of the motion event specifies
the class of input that was received. Implementations of this method
must examine the bits in the source before processing the event.
The following code example shows how this is done.
Generic motion events with source class
InputDevice.SOURCE_CLASS_POINTER
are delivered to the view under the pointer. All other generic motion events are
delivered to the focused view.
See View.onGenericMotionEvent(MotionEvent)
for an example of how to
handle this event.
event
- The generic motion event being processed.public void onWindowAttributesChanged(WindowManager.LayoutParams params)
Window.Callback
onWindowAttributesChanged
in interface Window.Callback
public void onContentChanged()
Window.Callback
Window.setContentView
or
Window.addContentView
).onContentChanged
in interface Window.Callback
public void onWindowFocusChanged(boolean hasFocus)
Window.Callback
View.onWindowFocusChanged(boolean)
for more information.onWindowFocusChanged
in interface Window.Callback
hasFocus
- Whether the window now has focus.public void onAttachedToWindow()
Window.Callback
View.onAttachedToWindow()
for more information.onAttachedToWindow
in interface Window.Callback
public void onDetachedFromWindow()
Window.Callback
View.onDetachedFromWindow()
for more information.onDetachedFromWindow
in interface Window.Callback
public boolean dispatchKeyEvent(KeyEvent event)
dispatchKeyEvent
in interface Window.Callback
event
- The key event.public boolean dispatchKeyShortcutEvent(KeyEvent event)
dispatchKeyShortcutEvent
in interface Window.Callback
event
- The key shortcut event.public boolean dispatchTouchEvent(MotionEvent ev)
dispatchTouchEvent
in interface Window.Callback
ev
- The touch screen event.public boolean dispatchTrackballEvent(MotionEvent ev)
dispatchTrackballEvent
in interface Window.Callback
ev
- The trackball event.public boolean dispatchGenericMotionEvent(MotionEvent ev)
dispatchGenericMotionEvent
in interface Window.Callback
ev
- The generic motion event.public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event)
Window.Callback
AccessibilityEvent
s.dispatchPopulateAccessibilityEvent
in interface Window.Callback
event
- The event.public View onCreatePanelView(int featureId)
Window.Callback
onCreatePanelView
in interface Window.Callback
featureId
- Which panel is being created.Activity.onCreatePanelView(int)
public boolean onCreatePanelMenu(int featureId, Menu menu)
Window.Callback
You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreatePanelMenu() is called for this feature.
onCreatePanelMenu
in interface Window.Callback
featureId
- The panel being created.menu
- The menu inside the panel.Activity.onCreatePanelMenu(int, Menu)
public boolean onPreparePanel(int featureId, View view, Menu menu)
Window.Callback
onPreparePanel
in interface Window.Callback
featureId
- The panel that is being displayed.view
- The View that was returned by onCreatePanelView().menu
- If onCreatePanelView() returned null, this is the Menu
being displayed in the panel.Activity.onPreparePanel(int, View, Menu)
public boolean onMenuOpened(int featureId, Menu menu)
Window.Callback
onMenuOpened
in interface Window.Callback
featureId
- The panel that the menu is in.menu
- The menu that is opened.Activity.onMenuOpened(int, Menu)
public boolean onMenuItemSelected(int featureId, MenuItem item)
Window.Callback
onMenuItemSelected
in interface Window.Callback
featureId
- The panel that the menu is in.item
- The menu item that was selected.Activity.onMenuItemSelected(int, MenuItem)
public void onPanelClosed(int featureId, Menu menu)
Window.Callback
onPanelClosed
in interface Window.Callback
featureId
- The panel that is being displayed.menu
- If onCreatePanelView() returned null, this is the Menu
being displayed in the panel.Activity.onPanelClosed(int, Menu)
public boolean onCreateOptionsMenu(Menu menu)
Activity.onCreateOptionsMenu(Menu)
if the client desires the same
menu for this Dialog.public boolean onPrepareOptionsMenu(Menu menu)
Activity.onPrepareOptionsMenu(Menu)
if the client desires the
same menu for this Dialog.public boolean onOptionsItemSelected(MenuItem item)
public void onOptionsMenuClosed(Menu menu)
Activity.onOptionsMenuClosed(Menu)
public void openOptionsMenu()
Activity.openOptionsMenu()
public void closeOptionsMenu()
Activity.closeOptionsMenu()
public void invalidateOptionsMenu()
Activity.invalidateOptionsMenu()
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
View.OnCreateContextMenuListener
onCreateContextMenu
in interface View.OnCreateContextMenuListener
menu
- The context menu that is being builtv
- The view for which the context menu is being builtmenuInfo
- Extra information about the item for which the
context menu should be shown. This information will vary
depending on the class of v.Activity#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)
public void registerForContextMenu(View view)
public void unregisterForContextMenu(View view)
public void openContextMenu(View view)
Activity.openContextMenu(View)
public boolean onContextItemSelected(MenuItem item)
public void onContextMenuClosed(Menu menu)
Activity.onContextMenuClosed(Menu)
public boolean onSearchRequested()
onSearchRequested
in interface Window.Callback
Activity.onSearchRequested()
public ActionMode onWindowStartingActionMode(ActionMode.Callback callback)
Window.Callback
onWindowStartingActionMode
in interface Window.Callback
callback
- Callback to control the lifecycle of this action modepublic void onActionModeStarted(ActionMode mode)
onActionModeStarted
in interface Window.Callback
mode
- The new mode that has just been started.public void onActionModeFinished(ActionMode mode)
onActionModeFinished
in interface Window.Callback
mode
- The mode that was just finished.public void takeKeyEvents(boolean get)
get
- true if the dialog should receive key events, false otherwiseWindow.takeKeyEvents(boolean)
public final boolean requestWindowFeature(int featureId)
getWindow().requestFeature()
.featureId
- The desired feature as defined in
Window
.Window.requestFeature(int)
public final void setFeatureDrawableResource(int featureId, int resId)
Window.setFeatureDrawableResource(int, int)
.public final void setFeatureDrawableUri(int featureId, Uri uri)
Window.setFeatureDrawableUri(int, android.net.Uri)
.public final void setFeatureDrawable(int featureId, Drawable drawable)
Window.setFeatureDrawable(int, Drawable)
.public final void setFeatureDrawableAlpha(int featureId, int alpha)
Window.setFeatureDrawableAlpha(int, int)
.public LayoutInflater getLayoutInflater()
public void setCancelable(boolean flag)
BACK
key.public void setCanceledOnTouchOutside(boolean cancel)
cancel
- Whether the dialog should be canceled when touched outside
the window.public void cancel()
dismiss()
, but it will
also call your DialogInterface.OnCancelListener
(if registered).cancel
in interface DialogInterface
public void setOnCancelListener(DialogInterface.OnCancelListener listener)
This will only be invoked when the dialog is canceled.
Cancel events alone will not capture all ways that
the dialog might be dismissed. If the creator needs
to know when a dialog is dismissed in general, use
setOnDismissListener(android.content.DialogInterface.OnDismissListener)
.
listener
- The DialogInterface.OnCancelListener
to use.public void setCancelMessage(Message msg)
msg
- The msg to send when the dialog is canceled.setOnCancelListener(android.content.DialogInterface.OnCancelListener)
public void setOnDismissListener(DialogInterface.OnDismissListener listener)
listener
- The DialogInterface.OnDismissListener
to use.public void setOnShowListener(DialogInterface.OnShowListener listener)
listener
- The DialogInterface.OnShowListener
to use.public void setDismissMessage(Message msg)
msg
- The msg to send when the dialog is dismissed.public boolean takeCancelAndDismissListeners(String msg, DialogInterface.OnCancelListener cancel, DialogInterface.OnDismissListener dismiss)
public final void setVolumeControlStream(int streamType)
public final int getVolumeControlStream()
Activity.getVolumeControlStream()
public void setOnKeyListener(DialogInterface.OnKeyListener onKeyListener)