Skip to content

SimpleDialog

Philipp Niedermayer edited this page Jun 22, 2018 · 25 revisions

SimpleDialog

  1. Call the build() method of the dialog
  2. Customize the dialog as needed using the methods described below
  3. Show the dialog via show()

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 with the alertDialogTheme-Attribute in your main App-Theme (see styles.xml in the example application).
  • 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, if you want 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 OnDialogResultListener to receive results.
Please note that before v2.3 the result listener was only called, if a tag was supplied to the show method. Starting with v2.3 the dialog's TAG field will be used as default tag (WARNING: Not passing a TAG is unreliable, see #42)

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

    if (MY_DIALOG_TAG.equals(dialogTag)){
        switch(which){
            case BUTTON_POSITIVE:
                // ...
                break;
            case BUTTON_NEGATIVE:
                // ...
                break;
            // ...
        }
        return true;
    }

    return false;
}

The extras Bundle will contain all extras supplied when building the dialog. Additional extras will be added depending on the dialog type (e.g. the entered text for an input-dialog). See type-specific documentation for details.

Return true from this method if the result was handled, false otherwise.