Chartsv26.10.18

Android applications

Embedding Unifin Charts into Android apps through a WebView - what works today and what the roadmap adds.

This page covers embedding the library into an Android app through a WebView: the page setup, the JavaScript bridge, a verified harness and the limits to know about.

Unifin Charts is a browser library: it renders on a canvas through standard web APIs, so a WebView runs it exactly like a desktop browser. No SDK is required.

How embedding works today

  1. Ship the page (or a small bundle) with your app assets, or serve it from your backend.
  2. Load it in a WebView with JavaScript and DOM storage enabled.
  3. Size the WebView to the desired chart area; the renderer adapts to any container size.
val webView = WebView(context).apply {
    settings.javaScriptEnabled = true
    settings.domStorageEnabled = true
    settings.mediaPlaybackRequiresUserGesture = false
    loadUrl("file:///android_asset/chart/index.html")
}
<!-- android_asset/chart/index.html -->
<div id="app" style="height: 100vh"></div>
<script type="module" src="./chart.js"></script>

Communication between the app and the chart uses the standard WebView bridge: addJavascriptInterface towards the page and evaluateJavascript (or URL schemes) back to the app.

What to check on device

AreaRecommendation
TouchThe element handles pointer input; verify pan, zoom and crosshair with the on-device gestures
FontsBundle fonts next to the assets - remote font loading is blocked on some networks
PerformanceSystem WebView versions vary; test frame timing on your oldest target device
ThemePass the app theme into the page and align tokens with your app palette
OfflineKeep data fetching in the app; feed bars into the page through the bridge

Harness example

The repository ships a ready harness at examples/android-webview: an offline page (assets only, no network), a tiny native bridge and a gradle sample that loads the page from file:///android_asset/index.html.

PieceWhat it does
src/bridge.mjswindow.UnifinHost.setBars(json), setTheme(dark|light), setSymbol(symbol, timeframe); page events unifin-ready and the onCrosshair hook
src/app.tsMounts the element full screen, forwards wheel/pinch zoom, repaints on resize
app/MainActivity.ktWebView with JavaScript and DOM storage, addJavascriptInterface, deterministic demo series
node build.mjsBuilds assets/app.js (single IIFE) and copies index.html next to it

Verified in automation: the bridge protocol tests (node --test), and a touch-enabled browser emulation that loads the built page, sends bars through the bridge, zooms with a wheel event and checks the cursor hook.

Checked on a device, confirm the same items: touch pan and zoom feel, cursor readout, fonts bundled with the assets and frame timing on your oldest target WebView. The harness page stays offline by design: keep data fetching in the native layer or in your own page.

Limitations to know

  • The JavascriptInterface bridge must only be exposed to the offline assets page; never enable it for pages that can navigate to the network.
  • Some Android builds restrict cookies (FedCM-era changes) - do not rely on cookie-based sessions inside the WebView; authenticate in the native layer and pass data through the bridge.
  • Very old System WebView versions lack CustomEvent; the harness falls back to the onUnifinBridgeEvent host hook.

Caution Keep the WebView on bundled assets. A page that can reach the network hands the JavaScript interface to remote code.

See also

On this page