Reputation: 14484
I have a simple webview that is loading some locally stored HTML. There are plain vanilla links in the HTML, like <a href="https://google.com"/>
. When a user taps on one of these links, I want the device browser to open.
This is the default behaviour for an Android webview and seems to work fine, however as soon as I call the webView.SetWebViewClient(webViewClient)
method, this default behaviour stops.
I've tried overriding the ShouldOverrideUrlLoading()
method(s) in my implementation of the WebViewClient
class to open the browser myself, but these methods are never getting called.
To summarise, like this, links in my HTML will open in the browser:
webView = new WebView(Activity);
//var webViewClient = new WebViewClient();
//webView.SetWebViewClient(webViewClient);
webView.LoadData(myHTML, "text/html", null);
But as soon as I uncomment those two lines, the links will open within the WebView.
Upvotes: 1
Views: 1712
Reputation: 1088
I'm not sure why your ShouldOverrideUrlLoading method is not being called in your class as I don't have all your code to look at. So, I put together a fully working example where you can control how the links are opened--whether in your webview view or another Android browser activity. I hope it helps!
WebView.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<WebView
android:id="@+id/webviewMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>
testwebview.html ("Assets/html/testwebview.html" in project)
<html>
<head>
<title>WebView Testing</title>
</head>
<body>
<h1>Select a link...</h1>
<br />
<a href="https://www.google.com">Google</a>
<br />
<a href="https://stackoverflow.com">Stack Overflow</a>
</body>
</html>
WebViewCustomOverrideActivity.cs
using Android.App;
using Android.OS;
using Android.Webkit;
using Android.Content;
namespace XamdroidMaster.Activities {
[Activity(Label = "Custom WebView Testing", MainLauncher = true)]
public class WebViewCustomOverrideActivity : Activity {
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.WebView);
WebView wv = FindViewById<WebView>(Resource.Id.webviewMain);
MyWebViewClient myWebViewClient = new MyWebViewClient();
wv.SetWebViewClient(myWebViewClient);
wv.LoadUrl("file:///android_asset/html/testwebview.html"); // "Assets/html/testwebview.html" in project
}
}
public class MyWebViewClient : WebViewClient {
public override bool ShouldOverrideUrlLoading(WebView view, string url) {
if (url.ToLower().Contains("google.com")) {
// Let my WebView load the page
return false;
}
// For all other links, launch another Activity that handles URLs
var intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(url));
view.Context.StartActivity(intent);
return true;
}
}
}
Upvotes: 1