Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms HybridWebview Always Opens in Browser

I've implemented a HybridWebView as described here: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/

For now, I have the url hardcoded in the renderer, like this:

Control.LoadUrl("http://www.google.com");

The result is the url opens, but always in a new browser, not embedded in my ContentPage as I would expect.

Is there a Settings property on the Android.Webkit.WebView I can use to determine how it displays?

like image 317
erin Avatar asked Nov 21 '25 05:11

erin


1 Answers

The result is the url opens, but always in a new browser, not embedded in my ContentPage as I would expect.

For android platform, it uses Android.Webkit.WebView as native control as you can see it from code:

if (Control == null) {
    var webView = new Android.Webkit.WebView (Forms.Context);
    webView.Settings.JavaScriptEnabled = true;
    SetNativeControl (webView);
}

Then for Android.Webkit.WebView, if you don't enable the activity to handle the intent to view a web page, it is a simple web page viewer, not quite a browser yet because as soon as you click a link, the default Android Browser handles it. To enable it, this code can go anywhere following the initialization of WebView: webView.SetWebViewClient(new WebViewClient()). So you may add this code for example:

if (Control == null) {
    var webView = new Android.Webkit.WebView (Forms.Context);
    webView.SetWebViewClient(new WebViewClient());
    webView.Settings.JavaScriptEnabled = true;
    SetNativeControl (webView);
}

Usually we can also subclass WebViewClient and override ShouldOverrideUrlLoading, if it returns true, means that method has handled the URL and the event should not propagate.

like image 163
Grace Feng Avatar answered Nov 22 '25 20:11

Grace Feng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!