Forum Xamarin.Forms

Xamarin Forms WebView wont open new window links on Android device

FlintFlint Member ✭✭
edited January 14 in Xamarin.Forms

Hi,

I have a WebView in my app that displays a webpage. That webpage has a link in this format (ie using target="_blank")
<a href="https://www.w3schools.com" target="_blank">Link</a>

When i click on the link in the app on my Android device nothing happens, no navigating event occurs. My intention for the link was to intercept it via the navigating event and launch it in the devices default web browser (as its an external link away from my site which is being displayed in the webview). Problem is I don't get the event (or anything else) on Android when the link is clicked.

On iOS this works fine.

I have tested both the latest 4.x Xamarin Forms and the 5.0 version as well.

The simplest way to repo is to create a XF apps and add the following webview to the main page

<WebView x:Name="webView" Source="https://www.w3schools.com/tags/att_a_target.asp" WidthRequest="1000" HeightRequest="1000" />

Run the app on Android and click the [Try it Yourself] button on the webpage, nothing happens. The debug console output just shows these lines each time i click the link, but nothing else happens.
01-14 15:35:06.378 D/[email protected][MainActivity](15009): ViewPostIme pointer 0
01-14 15:35:06.509 D/[email protected][MainActivity](15009): ViewPostIme pointer 1
01-14 15:35:06.554 D/InputMethodManager(15009): HSIFW - flag : 0

Any ideas, am I just missing something obvious?

Cheers

Best Answer

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited January 14 Accepted Answer

    If you set the using target="_blank", you could achieve the SetWebChromeClient as well. To open the browser create an new blank page for this needs.

      Control.SetWebChromeClient(new MywebviewChrome());
    
    internal class MywebviewChrome : Android.Webkit.WebChromeClient
        {
                public override bool OnCreateWindow(Android.Webkit.WebView view, bool isDialog, bool isUserGesture, Message resultMsg)
                {
                    Android.Webkit.WebView.HitTestResult result = view.GetHitTestResult();
                    string data = result.Extra;
                    Context context = view.Context;
                    Intent browserIntent = new Intent(Intent.ActionView, Uri.Parse(data));
                    context.StartActivity(browserIntent);
    
                    return false;
                }
    
    
    
    
        }
    

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited January 14

    You only need to set a WebViewClient for your WebView and then all links will be opened it the same WebView:

    Create an custom renderer for your webview,

    [assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]
    namespace WebViewTest.Droid
    {
        public class CustomWebViewRenderer : WebViewRenderer
        {
            public CustomWebViewRenderer(Context context) : base(context)
            {
    
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
            {
                base.OnElementChanged(e);
    
                if (Control != null)
                {
                  //  Control.Settings.AllowFileAccess = true;
                    var webViewClient = new CustomWebViewClient();
                    Control.SetWebViewClient(webViewClient);
    
                }
            }
    }
    

    And override ShouldOverrideUrlLoading

     public class CustomWebViewClient : WebViewClient
        {
    
    
            public override bool ShouldOverrideUrlLoading(WebView view, string url)
            {
                view.LoadUrl(url);
                return true;
               // return base.ShouldOverrideUrlLoading(view, url);
    
            }
        }
    }
    
  • FlintFlint Member ✭✭

    @LeonLu said:
    You only need to set a WebViewClient for your WebView and then all links will be opened it the same WebView:

    Create an custom renderer for your webview,

    Thanks for the reply. I had tried this before but didn't have any luck either. I was just trying to use a new WebViewClient. I notice in your example you use a new CustomWebViewClient, which I assume you must be doing something extra with that makes the difference. Could you please post your CustomWebViewClient implementation as well? Thanks

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited January 14 Accepted Answer

    If you set the using target="_blank", you could achieve the SetWebChromeClient as well. To open the browser create an new blank page for this needs.

      Control.SetWebChromeClient(new MywebviewChrome());
    
    internal class MywebviewChrome : Android.Webkit.WebChromeClient
        {
                public override bool OnCreateWindow(Android.Webkit.WebView view, bool isDialog, bool isUserGesture, Message resultMsg)
                {
                    Android.Webkit.WebView.HitTestResult result = view.GetHitTestResult();
                    string data = result.Extra;
                    Context context = view.Context;
                    Intent browserIntent = new Intent(Intent.ActionView, Uri.Parse(data));
                    context.StartActivity(browserIntent);
    
                    return false;
                }
    
    
    
    
        }
    
  • FlintFlint Member ✭✭

    @LeonLu said:
    If you set the using target="_blank", you could achieve the SetWebChromeClient as well. To open the browser create an new blank page for this needs.

    Thanks for that, that seems to have done the trick, the behaviour is exactly what I am after as well. Cheers

Sign In or Register to comment.