2

I'm trying to share some data from an Activity to JavaScript, but all my results have failed, this is my current code:

C#

var webView = FindViewById<WebView>(Resource.Id.webView);

webView.SetWebChromeClient(new WebChromeClient());

webView.Settings.JavaScriptCanOpenWindowsAutomatically = true;
webView.Settings.JavaScriptEnabled = true;
string script = string.Format("javascript:UpdateData('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}');", "1", "2", "3", "4", "5", "6", "7", "8", "9");

webView.LoadUrl("file:///android_asset/index.html");

if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
    webView.EvaluateJavascript(script, null);
}
else
{
    webView.LoadUrl(script);
}

JavaScript inside of index.html (I tried to add the code in the head and in the end of the body too):

<script type="text/javascript">
    function UpdateData(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) {
        alert("Hi");
    }
</script>

AndroidManifest.xml

<?xml version="1.0" encoding"utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Test.Test" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="19" />
    <application android:label="Test"></application>
  <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Honestly, I don't know what I'm doing wrong because nothing happens, the basic web page is rendered perfectly, but the JavaScript is not triggered. Has anyone experienced this before?

These are my configurations if they can be useful: Configuration

avd

1 Answer 1

5

I think the problem is that you are trying to evaluate the script immediately after you load the URL. After you call webView.LoadUrl(...), the WebView needs some time to fully load the page including the contained <script>.

What you need is to create a class derived from WebViewClient and override the OnPageFinished method.

public class JavaScriptWebViewClient : WebViewClient
{
    public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);

        string script = string.Format("javascript:UpdateData('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}');", "1", "2", "3", "4", "5", "6", "7", "8", "9");

        if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
        {
            view.EvaluateJavascript(script, null);
        }
        else
        {
            view.LoadUrl(script);
        }
    }
}

And your original code would then need to set the web client:

var webView = FindViewById<WebView>(Resource.Id.webView);
webView.SetWebChromeClient(new WebChromeClient());
webView.Settings.JavaScriptCanOpenWindowsAutomatically = true;
webView.Settings.JavaScriptEnabled = true;

//set the custom web client
webView.SetWebViewClient(new JavaScriptWebViewClient());

webView.LoadUrl("file:///android_asset/index.html");
Sign up to request clarification or add additional context in comments.

3 Comments

The best solution ever! Thanks!
very usefull ,thanks but i think there is some error in your answer, for example : if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)...
Oh, it should not be, the question was for Xamarin.Android, which is C# version of Android API, which has different naming conventions than Java :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.