Class DynamicDialog
java.lang.Object
android.app.Dialog
androidx.activity.ComponentDialog
androidx.appcompat.app.AppCompatDialog
com.pranavpandey.android.dynamic.dialogs.DynamicDialog
- All Implemented Interfaces:
DialogInterface
,KeyEvent.Callback
,View.OnCreateContextMenuListener
,Window.Callback
,OnBackPressedDispatcherOwner
,AppCompatCallback
,LifecycleOwner
,SavedStateRegistryOwner
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 DynamicDialog 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(android.os.Bundle)
.
Developer Guides
For more information about creating dialogs, read the Dialogs developer guide.
-
Nested Class Summary
Nested classes/interfaces inherited from interface android.content.DialogInterface
DialogInterface.OnCancelListener, DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnKeyListener, DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnShowListener
-
Field Summary
Fields inherited from interface android.content.DialogInterface
BUTTON_NEGATIVE, BUTTON_NEUTRAL, BUTTON_POSITIVE, BUTTON1, BUTTON2, BUTTON3
-
Constructor Summary
ModifierConstructorDescriptionprotected
DynamicDialog
(Context context) protected
DynamicDialog
(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) Construct an DynamicDialog that uses an explicit theme.protected
DynamicDialog
(Context context, int themeResId) Construct an DynamicDialog that uses an explicit theme. -
Method Summary
Modifier and TypeMethodDescriptiongetButton
(int whichButton) Gets one of the buttons used in the dialog.Gets the list view used in the dialog.getView()
Get the view displayed in the dialog.protected void
boolean
boolean
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, 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.void
setButton
(int whichButton, CharSequence text, Message msg) Sets a message to be sent when a button is pressed.void
setCustomTitle
(View customTitleView) This method has no effect if called afterDialog.show()
.void
setIcon
(int resId) Set resId to 0 if you don't want an icon.void
Set theDrawable
to be used in the title.void
setIconAttribute
(int attrId) Sets an icon as supplied by a theme attribute.void
setMessage
(CharSequence message) Sets the message to display.void
setTitle
(CharSequence title) void
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.void
setViewRoot
(View viewRoot) Set the view root to add scroll indicators if the content can be scrolled.Methods inherited from class androidx.appcompat.app.AppCompatDialog
addContentView, dismiss, dispatchKeyEvent, findViewById, getDelegate, getSupportActionBar, invalidateOptionsMenu, onStop, onSupportActionModeFinished, onSupportActionModeStarted, onWindowStartingSupportActionMode, setContentView, setContentView, setContentView, setTitle, supportRequestWindowFeature
Methods inherited from class androidx.activity.ComponentDialog
getLifecycle, getOnBackPressedDispatcher, getSavedStateRegistry, initializeViewTreeOwners, onBackPressed, onSaveInstanceState, onStart
Methods inherited from class android.app.Dialog
cancel, closeOptionsMenu, create, dispatchGenericMotionEvent, dispatchKeyShortcutEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, getActionBar, getContext, getCurrentFocus, getLayoutInflater, getOnBackInvokedDispatcher, getOwnerActivity, getSearchEvent, getVolumeControlStream, getWindow, hide, isShowing, onActionModeFinished, onActionModeStarted, onAttachedToWindow, onContentChanged, onContextItemSelected, onContextMenuClosed, onCreateContextMenu, onCreateOptionsMenu, onCreatePanelMenu, onCreatePanelView, onDetachedFromWindow, onGenericMotionEvent, onKeyLongPress, onKeyMultiple, onKeyShortcut, onMenuItemSelected, onMenuOpened, onOptionsItemSelected, onOptionsMenuClosed, onPanelClosed, onPrepareOptionsMenu, onPreparePanel, onRestoreInstanceState, onSearchRequested, onSearchRequested, onTouchEvent, onTrackballEvent, onWindowAttributesChanged, onWindowFocusChanged, onWindowStartingActionMode, onWindowStartingActionMode, openContextMenu, openOptionsMenu, registerForContextMenu, requestWindowFeature, requireViewById, setCancelable, setCanceledOnTouchOutside, setCancelMessage, setDismissMessage, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setOnCancelListener, setOnDismissListener, setOnKeyListener, setOnShowListener, setOwnerActivity, setVolumeControlStream, show, takeKeyEvents, unregisterForContextMenu
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface android.content.DialogInterface
cancel, dismiss
Methods inherited from interface android.view.Window.Callback
onPointerCaptureChanged, onProvideKeyboardShortcuts
-
Constructor Details
-
DynamicDialog
-
DynamicDialog
Construct an DynamicDialog that uses an explicit theme. The actual style that an DynamicDialog 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 asR.attr.alertDialogTheme
.- Parameters:
context
- The context for the dialog.themeResId
- The theme resource id fo the dialog.
-
DynamicDialog
protected DynamicDialog(@NonNull Context context, boolean cancelable, @Nullable DialogInterface.OnCancelListener cancelListener) Construct an DynamicDialog that uses an explicit theme. The actual style that an DynamicDialog 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 asR.attr.alertDialogTheme
.- Parameters:
context
- The context for the dialog.cancelable
-true
to set the dialog cancelable.cancelListener
- The cancel listener to be set.
-
-
Method Details
-
getButton
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, viaDialog.show()
orDialog.create()
).- Parameters:
whichButton
- The identifier of the button that should be returned. For example, this can beDialogInterface.BUTTON_POSITIVE
.- Returns:
- The button from the dialog, or null if a button does not exist.
-
getListView
Gets the list view used in the dialog.- Returns:
- The
ListView
from the dialog.
-
setTitle
- Overrides:
setTitle
in classAppCompatDialog
-
setCustomTitle
This method has no effect if called afterDialog.show()
.- Parameters:
customTitleView
- The custom title view for the dialog.- See Also:
-
setMessage
Sets the message to display.- Parameters:
message
- The message to display in the dialog.
-
getView
Get the view displayed in the dialog. -
setView
Set the view to display in the dialog. This method has no effect if called afterDialog.show()
.- Parameters:
view
- The view to show in the content area of the dialog.
-
setView
public void setView(@Nullable 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 afterDialog.show()
.- Parameters:
view
- The view to show in the content area of the dialogviewSpacingLeft
- Extra space to appear to the left ofview
viewSpacingTop
- Extra space to appear aboveview
viewSpacingRight
- Extra space to appear to the right ofview
viewSpacingBottom
- Extra space to appear belowview
-
setViewRoot
Set the view root to add scroll indicators if the content can be scrolled.This method has no effect if called after
Dialog.show()
.- Parameters:
viewRoot
- The view root to be set.
-
setButton
Sets a message to be sent when a button is pressed. This method has no effect if called afterDialog.show()
.- Parameters:
whichButton
- Which button to set the message for, can be one ofDialogInterface.BUTTON_POSITIVE
,DialogInterface.BUTTON_NEGATIVE
, orDialogInterface.BUTTON_NEUTRAL
text
- The text to display in positive button.msg
- TheMessage
to be sent when clicked.
-
setButton
Sets a listener to be invoked when the positive button of the dialog is pressed. This method has no effect if called afterDialog.show()
.- Parameters:
whichButton
- Which button to set the listener on, can be one ofDialogInterface.BUTTON_POSITIVE
,DialogInterface.BUTTON_NEGATIVE
, orDialogInterface.BUTTON_NEUTRAL
text
- The text to display in positive button.listener
- TheDialogInterface.OnClickListener
to use.
-
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 afterDialog.show()
.- Parameters:
whichButton
- Which button to set the listener on, can be one ofDialogInterface.BUTTON_POSITIVE
,DialogInterface.BUTTON_NEGATIVE
, orDialogInterface.BUTTON_NEUTRAL
text
- The text to display in positive button.listener
- TheDialogInterface.OnClickListener
to use.icon
- TheDrawable
to be set as an icon for the button.
-
setIcon
public void setIcon(int resId) Set resId to 0 if you don't want an icon.- Parameters:
resId
- the resourceId of the drawable to use as the icon or 0 if you don't want an icon.
-
setIcon
Set theDrawable
to be used in the title.- Parameters:
icon
- Drawable to use as the icon or null 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:
attrId
- ID of a theme attribute that points to a drawable resource.
-
onCreate
- Overrides:
onCreate
in classAppCompatDialog
-
onKeyDown
- Specified by:
onKeyDown
in interfaceKeyEvent.Callback
- Overrides:
onKeyDown
in classDialog
-
onKeyUp
- Specified by:
onKeyUp
in interfaceKeyEvent.Callback
- Overrides:
onKeyUp
in classDialog
-