Even I have defined Android as a javascripinterface
webView.addJavascriptInterface(this, "Android");
I am using kotlin btw.
my .html file that I have loaded in a view.
<html>
<head>
</head>
<body>
<input type="button" onclick="showAndroidToast('Hello Android')" value="Hello"/>
<script type="text/javascript">
function showAndroidToast(toast) {
if(typeof Android !== "undefined" && Android !== null) {
android.showToast(toast);
} else {
alert("Not viewing in webview");
}
}
</script>
</body>
</html>
==============================================================
I have setted webview settings
webview.settings.javaScriptEnabled = true
webview.setBackgroundColor(Color.parseColor("#808080"))
//Set whether the DOM storage API is enabled.
webview.settings.domStorageEnabled = true
//setBuiltInZoomControls = false, removes +/- controls on screen
webview.settings.builtInZoomControls = false
webview.settings.allowFileAccess = true
webview.settings.setAppCacheEnabled(true);
jsHandler = JsHandler(this@UserRegistrationActivity,webview)
webview.webChromeClient = object :WebChromeClient(){
override fun onConsoleMessage(cm: ConsoleMessage?): Boolean {
Log.d("CONTENT", String.format("%s @ %d: %s",
cm?.message(), cm?.lineNumber(), cm?.sourceId()));
return true
}
}
webview.webViewClient = object : WebViewClient(){
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
}
override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
super.onReceivedSslError(view, handler, error)
}
}
webview.settings.cacheMode = WebSettings.LOAD_NO_CACHE
webview.requestFocus(View.FOCUS_DOWN)
webview.loadUrl("file:///android_asset/jscript.html");
webview.addJavascriptInterface(WebAppInterface(this@UserRegistrationActivity),"Android")
I am not sure about it if anyone can help
Thanks in Advance