This plugin provides a simple way to display native platform “Toast” messages (short, non-blocking pop-up messages common on Android) or similar feedback mechanisms.

Features

  • Display toasts from native code.
  • Display toasts triggered from JavaScript running in the WebView.
  • Configurable default duration (Short/Long).

Setup & Initialization

The `ToastPlugin.java` class uses a static initializer block to automatically register itself with the `PluginManager` when the class is loaded. No manual registration is typically required.
Its default configuration (`defaultDuration`) can be overridden in `Playground.java`:
<CodeBlock language="java" title="Playground.java (initializePluginDefaults)">
Map<String, Object> toastConfig = new HashMap<>();
// Override default: Toast.LENGTH_LONG or Toast.LENGTH_SHORT
toastConfig.put("defaultDuration", Toast.LENGTH_LONG);
savePluginConfig("ToastPlugin", toastConfig);
</CodeBlock>

During initialization (`initialize` method), the plugin:
1. Stores the `Activity` context.
2. Reads the `defaultDuration` from the provided configuration.
3. Adds a JavaScript interface named `ToastInterface` to the WebView, allowing web content to call native toast methods.
4. In `onPageFinished`, it injects helper JavaScript (`window.Toast.show`, `window.Toast.showLong`) that uses the `ToastInterface`.
```md ```

Usage

From Native Code

1. Get the plugin instance from the `PluginManager`. 2. Call the `showToast` methods.
<CodeBlock language="java" title="Example (e.g., in Playground or another class)">
Object plugin = SmartWebView.getPluginManager().getPluginInstance("ToastPlugin");
if (plugin instanceof mgks.os.swv.plugins.ToastPlugin toastPlugin) {
    // Show toast with default duration (set via config)
    toastPlugin.showToast("Hello from Native!");

    // Show toast with specific duration
    toastPlugin.showToast("This is a long toast", Toast.LENGTH_LONG);
}
</CodeBlock>
```md ```

From JavaScript

After the page finishes loading (`onPageFinished`), the plugin injects a `window.Toast` object. You can call its methods directly from your web page's JavaScript.
<CodeBlock language="javascript" title="Web Page JavaScript">
// Ensure the interface is ready (optional check)
if (window.Toast) {
  // Show toast with default duration
  window.Toast.show("Hello from JavaScript!");

  // Show toast with long duration
  window.Toast.showLong("This JavaScript toast stays longer.");
} else {
  console.log("Toast interface not available yet.");
}

// Alternatively, call the raw interface directly (if needed)
if (window.ToastInterface) {
    window.ToastInterface.showToast("Direct interface call");
}
</CodeBlock>
```md ```

This plugin provides a basic but useful way to give quick feedback to the user, bridging native UI elements with web content interactions.