Customize alert dialogs

The super app can customize the alert dialog appearance in mini programs to ensure a consistent user interface. These dialogs notify users about important information, warnings, or confirmations. They support the following optional elements on Android:

  • A title
  • A message (might support configurable alignment)
  • One or two action buttons

For scenarios where alert dialogs appear, refer to Alert dialog appearance scenarios. To customize alert dialogs, the super app must implement the GriverDialogExtension interface. This topic covers the default user experience, implementation steps, and interface details.

Default user experience

By default, an alert dialog appears as a centered popup with a white background. It displays a title in black text at the top, a message in black text in the middle, and action buttons with blue text at the bottom. The following table shows some UI examples of different alert dialogs:

Alert with single button (center-aligned)

Alert with single button (left-aligned)

Alert with single button (right-aligned)

Alert with two buttons (center-aligned)

image-1.png

image-2.png

image-3.png

image-4.png

Procedure

To customize alert dialogs, take the following two steps:

Step 1: Implement the GriverDialogExtension interface

Create a class that implements the GriverDialogExtension interface. Within the class, override the createDialog and applyShow methods to customize an alert dialog. Refer to the following code for a sample implementation. For more information about the interface and the method, refer to GriverDialogExtension.

copy
class CustomDialogExtensionImpl : GriverDialogExtension {
    override fun createDialog(activity: Activity, createParam: CreateDialogParam?): Dialog? {
        return null;
        // Implement the logic to build a custom alert dialog
    }

    override fun applyShow(dialog: Dialog?, createDialogParam: CreateDialogParam?) {
        // Implement logic to further customize the dialog
    }
}

Step 2: Register the implementation class

After initializing the SDK, call the registerExtension API to register the implementation class (for example, CustomDialogExtensionImpl in the sample) with the SDK. Refer to the following sample registration code:

copy
IAPConnect.init(application, initConfig, object : InitCallback {
    fun onSuccess() {
        //···
        Griver.registerExtension(
            GriverExtensionManifest(
                GriverDialogExtension::class.java,
                CustomDialogExtensionImpl()
            )
        )
    }
})

Structures

GriverDialogExtension interface

The GriverDialogExtension interface defines methods for the super app to customize an alert dialog, which the SDK then calls to render the custom dialog. Refer to the following code for the interface definition:

copy
interface GriverDialogExtension : GriverExtension {
    
    fun createDialog(activity: Activity?, createParam: CreateDialogParam?): Dialog?

    fun applyShow(dialog: Dialog?, createDialogParam: CreateDialogParam?)
}

Based on the definition, this interface provides the following methods:

Method

Description

createDialog

Called by the SDK to create a custom dialog instance. For more information, refer to createDialog.

applyShow

Called by the SDK after the dialog is shown to enable further customization because some elements can be obtained only after dialog creation. For more information, refer to applyShow.

createDialog method

Parameters

The createDialog method has the following input parameters:

Parameter

Data type

Required

Description

activity

Activity

No

The Activity where the mini program is hosted.

createParam

CreateDialogParam

No

An object that contains the configuration for the custom alert dialog. For more information, refer to CreateDialogParam.

applyShow method

Parameters

The applyShow method has the following input parameters:

Parameter

Data type

Required

Description

dialog

Dialog

No

The dialog instance created by the createDialog method.

createDialogParam

CreateDialogParam

No

An object that contains the configuration for the custom alert dialog. For more information, refer to CreateDialogParam.

CreateDialogParam class

The CreateDialogParam class includes the following parameters:

Parameter

Data type

Required

Description

title

String

No

The title of the alert dialog.

message

String

No

The message of the alert dialog.

align

String

No

The message alignment. Valid values are:

  • left: Left-aligned.
  • right: Right-aligned.
  • center (default): Center-aligned.

positiveString

String

No

Text for the positive button (e.g., OK).

negativeString

String

No

Text for the negative button (e.g., Cancel).

positiveTextColor

String

No

The color of the positive button text in hexadecimal RGB or ARGB format (e.g., #RRGGBB or #AARRGGBB).

negativeTextColor

String

No

The color of the negative button text in hexadecimal RGB or ARGB format (e.g., #RRGGBB or #AARRGGBB).

positiveListener

DialogInterface.OnClickListener

No

The listener for positive button clicks. When the positive button is clicked, call the onClick method and pass in the dialog object and either of the following identifiers of the clicked button:

  • ex. DialogInterface.BUTTON_POSITIVE
  • the button position

negativeListener

DialogInterface.OnClickListener

No

The listener for negative button clicks. When the negative button is clicked, call the onClick method and pass in the dialog object and either of the following identifiers of the clicked button:

  • ex. DialogInterface.BUTTON_NEGATIVE
  • the button position

cancelListener

DialogInterface.OnCancelListener

No

The listener for dialog cancellation. When the dialog is canceled by clicking the back key, call the onCancel method and pass in the dialog object.

cancelable

Boolean

No

Indicate whether the dialog is cancelable by clicking the back key (KeyEvent#KEYCODE_BACK). Valid values are:

  • ture: The dialog is cancelable.
  • false (default): The dialog is not cancelable.

Appendices

Alert dialog appearance scenarios

An alert dialog appears when a mini program invokes one of the following JSAPIs and the associated scenarios:

Type

JSAPI name

Scenarios

SDK-provided JSAPIs

my.alert

Every call

my.confirm

Every call

my.saveImage

Confirming image save

my.addPhoneContact

Confirming contact save

my.showAuthGuide

Requesting permissions

my.chooseImage

User denies camera or storage access

my.chooseVideo

User denies camera or storage access

HTML5 JSAPIs

window.alert

Every call

window.confirm

Every call

window.prompt

Every call