Skip to content

SimpleDialog

Philipp Niedermayer edited this page Jun 12, 2021 · 25 revisions

SimpleDialog

Full API reference

This is the base class for all dialogs in this library. This wiki page describes the basic usage which applies to all derived subclasses.

To use a dialog:

  1. Call the static build() method of the dialog
  2. Customize the dialog as needed using the methods described below
  3. Show the dialog via show() or show(this, TAG)
  4. Retrieve results with the OnDialogResultListener as described below

Building dialogs

Example

SimpleDialog.build()
            .title(R.string.hello)
            .msg(R.string.hello_world)
            .show(Activity.this);

Available customizations

  • Title
    .title(String title), .title(int resId)
  • Message
    .msg(String message), .msg(int resId)
    You can also pass styled strings containing html tags using one of:
    .msgHtml(String message), .msgHtml(int resId)
  • Buttons
    .pos(String label), .pos(int resId) (positive button)
    .neg(String label), .neg(int resId), .neg() (negative button)
    .neut(String label), .neut(int resId), .neut() (neutral button)
    Pass null to hide a button. The positive Button is shown by default unless you hide it.
  • Icon
    icon(int resId)
  • Cancelable
    cancelable(boolean cancelable)
    Controls weather the dialog may be suppressed by touching outside of it.
  • Theme
    theme(int resId)
    Set custom themes on a per-dialog-basis. You can also specify a global theme, see Styles.
  • Extras
    extra(Bundle extras)
    Supply additional extras that are added to the results (see Receiving results)

Showing dialogs

Use these methods to display the dialog in your activity or fragment.
The tag is required to get results from your dialog (see Receiving results). The methods without a tag are only meant for simple informative dialogs.

show(Fragment fragment);
show(Fragment fragment, String tag);
show(FragmentActivity activity);
show(FragmentActivity activity, String tag);

Receiving results

Let your activity or fragment implement the SimpleDialog.OnDialogResultListener to receive results.
Please note that the result listener is only called, if a tag was supplied to the show method. In a future version, we might use the dialog's TAG attribute as the default tag.

Parameters:

  • dialogTag - the tag passed to SimpleDialog.show(...)
  • which - result type, one of BUTTON_POSITIVE, BUTTON_NEGATIVE, BUTTON_NEUTRAL, CANCELED
  • extras - Bundle containing all extras supplied via SimpleDialog.extra(...) when the dialog was built. Additional extras will be added depending on the dialog type (e.g. the entered text for an input-dialog). See dialog specific documentation for details.

You should return true if the result was handled, otherwise the results are passed on to the next fragment in the fragment hierarchy up to the hosting activity until they got handled.

@Override
public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle extras) {

    if (MY_DIALOG_TAG.equals(dialogTag)){

        // get extras if applicable, i.e. user input
        // int color = extras.getInt(SimpleColorDialog.COLOR);

        switch(which){
            case BUTTON_POSITIVE:
                // ...
                break;
            case BUTTON_NEGATIVE:
                // ...
                break;
            // ...
        }
        return true;  // dialog result was handeled
    }
    return false;
}

Clone this wiki locally