Skip to content

SimpleDialog

Eltos edited this page Oct 29, 2021 · 25 revisions

SimpleDialog

API reference Examples

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

Usage

Building dialogs

A dialog is usually build by calling it's static build() method, customizing it as needed, and then showing the dialog with one of the show methods:

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

Depending on the dialog type, alternative buildXxx() methods may be available providing useful presets.

Customizing the dialog

For customization, various methods are provided to adjust the dialog's title (title), message (msg or msgHtml), icon (icon), buttons (pos, neg, neut), behaviour (cancelable) etc. Please refer to the API reference for a comprehensive documentation of these methods.

For dialog themes and styles see Styles.

Showing the dialog

The following methods exists to show a dialog either from a Fragment or Activity.
The tag is required to get and match results from your dialog (see Receiving results) and if you want to get the dialog at a later stage with FragmentManager#findFragmentByTag. The methods without a tag are only meant for simple informative dialogs.
An optional replaceTag can be supplied to make the dialog replace a previously shown dialog.

show​(androidx.fragment.app.Fragment fragment)
show​(androidx.fragment.app.Fragment fragment, String tag)
show​(androidx.fragment.app.Fragment fragment, String tag, String replaceTag)
show​(androidx.fragment.app.FragmentActivity activity)
show​(androidx.fragment.app.FragmentActivity activity, String tag)
show​(androidx.fragment.app.FragmentActivity activity, String tag, String replaceTag)

Receiving results

The SimpleDialog.OnDialogResultListener interface exists to receive and match results from a dialog, i.e. which button was pressed, any data entered or extras supplied during dialog creation.

public class MyActivity extends AppCompatActivity implements SimpleDialog.OnDialogResultListener {

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

If a dialog was shown with a tag, the onResult callback of the hosting fragment or activity will be called. If it returns false or if the callback is not implemented at all, the dialog will try calling the onResult callback for any fragment in the hierarchy up to the hosting activity.

The onResult callback provides three parameters:

  • dialogTag - the tag set when the dialog was shown (see Showing the dialog)
  • which - result type, one of BUTTON_POSITIVE, BUTTON_NEGATIVE, BUTTON_NEUTRAL or CANCELED as defined in OnDialogResultListener
  • extras - a bundle containing all extras supplied via SimpleDialog.extra(...) when the dialog was built and additional extras depending on the dialog type (e.g. the entered text for an input-dialog). See dialog specific wiki pages for details.

If the result was handled, you should return true to stop result propagation.

Example

@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);

        if (which == BUTTON_POSITIVE){
            // ...
        }
		
        return true;  // dialog result was handled
    }
    return false;
}

Note: Currently the onResult callback is never called if no tag was supplied to the dialog's show method. In a future version, we might use the dialog's TAG attribute as the default tag and call onResult with this default tag if no tag was supplied to the dialog's show method.

Examples

SimpleDialog.build()
            .title(R.string.hello)
            .msg(R.string.hello_world)
            .show(this);
SimpleDialog.build()
            .title("HTML styled text sample")
            .msgHtml("<h1>Header 1</h1><h2>Header 2</h2>" +
                     "This is an HTML text with <b>bold</b>, " +
                     "<span style=\"color:red;\">colored</span> " +
                     "and <strike>other</strike> formatting.")
            .show(this);
SimpleDialog.build()
            .title(R.string.alert_dialog)
            .msgHtml(R.string.ask_changes_discard)
            .pos(R.string.save)
            .neg(R.string.discard)
            .neut(R.string.long_cancel)
            .show(this, DIALOG_TAG);
SimpleDialog.build()
            .title(R.string.message)
            .msg(R.string.hello_world)
            .theme(R.style.MyFancyDialogTheme)
            .show(this);