Class DynamicDialog

All Implemented Interfaces:
DialogInterface, KeyEvent.Callback, View.OnCreateContextMenuListener, Window.Callback, OnBackPressedDispatcherOwner, AppCompatCallback, LifecycleOwner, SavedStateRegistryOwner

public class DynamicDialog extends AppCompatDialog implements DialogInterface
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.

  • Constructor Details

    • DynamicDialog

      protected DynamicDialog(@NonNull Context context)
    • DynamicDialog

      protected DynamicDialog(@NonNull Context context, @StyleRes int themeResId)
      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 as R.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 as R.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

      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 Dialog.show() or Dialog.create()).
      Parameters:
      whichButton - The identifier of the button that should be returned. For example, this can be DialogInterface.BUTTON_POSITIVE.
      Returns:
      The button from the dialog, or null if a button does not exist.
    • getListView

      public ListView getListView()
      Gets the list view used in the dialog.
      Returns:
      The ListView from the dialog.
    • setTitle

      public void setTitle(CharSequence title)
      Overrides:
      setTitle in class AppCompatDialog
    • setCustomTitle

      public void setCustomTitle(@Nullable View customTitleView)
      This method has no effect if called after Dialog.show().
      Parameters:
      customTitleView - The custom title view for the dialog.
      See Also:
    • setMessage

      public void setMessage(CharSequence message)
      Sets the message to display.
      Parameters:
      message - The message to display in the dialog.
    • getView

      @Nullable public View getView()
      Get the view displayed in the dialog.
    • setView

      public void setView(@Nullable View view)
      Set the view to display in the dialog. This method has no effect if called after Dialog.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 after Dialog.show().
      Parameters:
      view - The view to show in the content area of the dialog
      viewSpacingLeft - 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
    • setViewRoot

      public void setViewRoot(@Nullable View viewRoot)
      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

      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 Dialog.show().
      Parameters:
      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.
    • 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 Dialog.show().
      Parameters:
      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.
    • 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 Dialog.show().
      Parameters:
      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.
      icon - The Drawable 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

      public void setIcon(Drawable icon)
      Set the Drawable 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

      protected void onCreate(Bundle savedInstanceState)
      Overrides:
      onCreate in class AppCompatDialog
    • onKeyDown

      public boolean onKeyDown(int keyCode, @NonNull KeyEvent event)
      Specified by:
      onKeyDown in interface KeyEvent.Callback
      Overrides:
      onKeyDown in class Dialog
    • onKeyUp

      public boolean onKeyUp(int keyCode, @NonNull KeyEvent event)
      Specified by:
      onKeyUp in interface KeyEvent.Callback
      Overrides:
      onKeyUp in class Dialog