Toast Plugin
The ToastPlugin is included as a basic example of how the plugin architecture works. It provides a simple way to display native “Toast” messages (short, non-blocking pop-ups).
Features
- Display toasts from native Java code.
- Display toasts triggered from JavaScript in the WebView.
- Configurable default duration (short or long).
Setup & Configuration
Enable Plugin: Ensure
ToastPluginis listed in theplugins.enabledproperty inapp/src/main/assets/swv.properties.plugins.enabled=...,ToastPluginInternal Logic: The
ToastPlugin.javaclass uses a static initializer block to automatically register itself. During initialization, it adds a JavaScript interface namedToastInterfaceto the WebView.
Usage
From Native Code
- Get the plugin instance from the
PluginManager. - Call its
showToastmethod.
// Example from another class, like Playground.java
PluginInterface plugin = SWVContext.getPluginManager().getPluginInstance("ToastPlugin");
if (plugin instanceof mgks.os.swv.plugins.ToastPlugin) {
((mgks.os.swv.plugins.ToastPlugin) plugin).showToast("Hello from Native!");
}
From JavaScript
After the page has loaded, you can call the methods of the injected window.Toast helper object.
// Check if the interface is ready
if (window.Toast) {
// Show a toast with the default duration
window.Toast.show("Hello from JavaScript!");
// Show a toast with a long duration
window.Toast.showLong("This JavaScript toast stays for longer.");
}