1

I develop an app on Xamarin. I try to hide image when webview page loaded. I try different methods like call public function or access imageview from another class.

I read This but it doesn't work on Xamarin.

So I try something that

[Activity(Label = "XamarinWebView", Theme = "@android:style/Theme.Black.NoTitleBar", MainLauncher = true)]
     public class MainActivity : Activity
      {
         WebView app_view = null;
         WebSettings app_web_settings = null;
         WebChromeClient web_client;
         MyWebViewClient my_web_client;
         ImageView my_splash = null;

       protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.Main);
                app_view = FindViewById(Resource.Id.webViewapp) as WebView;
                my_web_client = new MyWebViewClient(this.ApplicationContext);
                app_view.SetWebViewClient(my_web_client);
                string app_url = "file:///android_asset/app_pages/test.html";
                app_view.LoadUrl(app_url);

                my_splash = FindViewById(Resource.Id.imageSplash) as ImageView;
 my_splash.SetImageDrawable(GetDrawable(Resource.Drawable.splash));
            }

        public void HideSplash()
        {
             my_splash.Visibility = ViewStates.Gone;
        }

    }

In this class I can get WebView page loading status.

public class MyWebViewClient : WebViewClient
    { 
        Context context;
        public MyWebViewClient(Context _context) {
            this.context = _context;
        }

        public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
        {
            base.OnPageStarted(view, url, favicon);
        }

        public override void OnPageFinished(WebView view, string url)
        {
            base.OnPageFinished(view, url);
            **I need to change visible my_splash**
        }

    }
BK52
  • 754
  • 2
  • 11
  • 33
  • 2
    the approach you linked to WILL work in Xamarin - just pass in a ref to your MainActivity when you create your webclient. Alternately, have your webclient raise an event when it completes, and your MainActivity can subscribe to it and responsd – Jason May 17 '18 at 19:28

2 Answers2

0

With Jason's advice I try something that and it works.

  public class MainActivity : Activity
    {
     protected override void OnCreate(Bundle savedInstanceState)
        {
           base.OnCreate(savedInstanceState);
           .
           .
           .
           my_web_client = new MyWebViewClient(this);
           .
           .
           .
       }

    public void HideSplash()
        {
             my_splash.Visibility = ViewStates.Gone;
        }
   }

 public class MyWebViewClient : WebViewClient
    {
        MainActivity act;
        public MyWebViewClient(MainActivity activity)
        {
            this.act = activity;
        }

        public override void OnPageFinished(WebView view, string url)
        {
            base.OnPageFinished(view, url);
            act.Hide_Splash();
        }
    }
BK52
  • 754
  • 2
  • 11
  • 33
0

A much easier approach to getting the activity from another class, which is also less error-prone and less likely to cause memory leaks would be to get the activity from the view context.

In your MyWebViewClient class:

public override void OnPageFinished(WebView view, string url)
{
    base.OnPageFinished(view, url);
    (view.Context as MainActivity).HideSplash();
}

Or if you're not sure if the context will always be MainActivity you could use pattern matching which covers the null check:

public override void OnPageFinished(WebView view, string url)
{
    base.OnPageFinished(view, url);
    if(view.Context is MainActivity mainActivity)
    {
        mainActivity.HideSplash();
    }
}

Using an approach like this is much easier to maintain down the road.

Matthew Andrews
  • 437
  • 1
  • 5
  • 16