Skip to content

SimpleDialog

Philipp edited this page Jan 2, 2017 · 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)
  • Buttons
    Positive button: .pos(String label), .pos(int resId)
    Negative button: .neg(String label), .neg(int resId), .neg()
    Neutral button: .neut(String label), .neut(int resId), .neut()
    Pass null to hide a button.
  • 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 you main App-Theme.
  • Extras
    extra(Bundle extras)
    Supply additional data that are added to the results (see Receiving results)

Showing dialogs

Use these methods to display the dialog in your activity or fragment. Supply an optional tag if you want to match results to your dialog (see Receiving results)

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

Receiving results

Let your activity or fragment implement the OnDialogResultListener to receive results:

    @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. For other dialog types also values such as a text that was entered will be included. See the documentation of these dialogs for details.

Clone this wiki locally