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 |
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
.
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.
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:
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:
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 |
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:
|
resultCallback | 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
.
interface GriverToastResultCallback {
fun onSuccess()
}