I am currently working on developing an Android application using Xamarin. One feature of the app involves a webview that needs to load upon the app's creation. Once the webview is fully loaded, I need to trigger some JavaScript code within the HTML page in order to set up a graph.
To achieve this, I have been attempting to utilize a custom WebChromeClient
and override the OnProgressChanged
method. When the webview finishes loading, I want it to call a specific method within my MainActivity
.
This is what the code in my MainActivity
looks like:
using System;
using System.Text;
using System.Timers;
using System.Collections;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Text;
using Android.Text.Style;
using Android.Webkit;
public class MainActivity : Activity
{
WebView graph;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
graph = FindViewById<WebView>(Resource.Id.webGraph);
//Initialize the WebView
graph.SetWebChromeClient(new MyWebChromeClient());
graph.Settings.JavaScriptEnabled = true;
graph.LoadUrl("file:///android_asset/graph.html");
The custom MyWebChromeClient
class I created is structured as follows:
class MyWebChromeClient : WebChromeClient
{
public override void OnProgressChanged(WebView view, int newProgress)
{
base.OnProgressChanged(view, newProgress);
if (newProgress == 100) {MainActivity.SetupGraph();}
}
}
Despite having the SetupGraph
method declared as public within the MainActivity
, I am encountering difficulties accessing it. Any assistance with resolving this issue would be greatly appreciated!