Customize toasts

Toasts are brief notifications that appear on the user interfaces of mini programs to provide feedback about events and actions, such as confirming success, informing about failures, alerting to errors, or offering quick informational updates.

This guide helps super apps customize toasts to ensure a consistent UI experience. Toasts are displayed when mini programs call the my.showToast JSAPI.

Default user experience

The following table shows the UI examples of default toasts.

Success

Fail

Exception

None

image

image

image

image

Before you begin

To customize the user interface for toasts, ensure that the integrated Android IAPMiniProgram SDK is version 2.65.1 or later. For more information, see SDK release notes.

Procedure

To customize toasts, take the following steps:

Step 1: Implement GriverToastExtension

Create a class that implements the GriverToastExtension interface. Within the class, use the showToast method to define a custom toast. For details about the GriverToastExtension interface, see GriverToastExtension.

copy
class CustomToastExtensionImpl : GriverToastExtension {

    override fun showToast(
        activityWeakReference: WeakReference<Activity>,
        content: String,
        duration: Int,
        type: String?,
        resultCallback: GriverToastResultCallback
    ) {
        //Implement the logic to customize a toast.
    }

    override fun hideToast() {
        //Implement the logic to hide a toast.
    }
}

Step 2: Register CustomToastExtensionImpl

After initializing the SDK, call the registerExtension API to register the CustomToastExtensionImpl class. In the following code sample, replace CustomToastExtensionImpl with the actual name of your implementation class.

copy
IAPConnect.init(application, initConfig, object : InitCallback {
    override fun onSuccess() {
        //ยทยทยท
        Griver.registerExtension(GriverExtensionManifest(
            GriverToastExtension::class.java,
            CustomToastExtensionImpl()
        ))
    }
})

Interface

GriverToastExtension

The following code shows the definition of the GriverToastExtension interface:

copy
interface GriverToastExtension : GriverExtension {

    @ThreadType(ExecutorType.UI)
    fun showToast(
        activityWeakReference: WeakReference<Activity>,
        content: String,
        duration: Int,
        type: String,
        resultCallback: GriverToastResultCallback
    )

    
    @ThreadType(ExecutorType.UI)
    fun hideToast()
}

The interface defines two methods. The following table lists the details:

Method

Required

Description

showToast

Yes

The method that is called by the SDK to render a custom toast.

hideToast

Yes

The method that is called by the SDK to hide a toast.

showToast

The showToast method has the following input parameters whose values are passed by the SDK:

Parameter

Data type

Required

Description

activityWeakReference

WeakReference<Activity>

Yes

A weak reference to Activity that provides the context required for UI rendering.

content

String

Yes

The toast content.

duration

int

Yes

The duration for which the toast remains visible, in milliseconds.

type

String

Yes

The toast type. Valid values:

  • success: The operation or task is completed successfully. A success icon is displayed.
  • fail: The operation or task is failed. A failure icon is displayed.
  • exception: An exception or unexpected error occurs. An exception icon is displayed.
  • none: The toast is generic. No state icon is displayed.

resultCallback

GriverToastResultCallback

Yes

The callback that handles results related to the toast operation.

hideToast

The hideToast method does not have input parameters.

Appendices

GriverToastResultCallback

The following code shows the definition of GriverToastResultCallback.

copy
interface GriverToastResultCallback {
    fun onSuccess()
}