erin
erin

Reputation: 665

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?

Upvotes: 3

Views: 690

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

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.

Upvotes: 6

Related Questions