question

MohamediAIJH-8327 avatar image
0 Votes"
MohamediAIJH-8327 asked LeonLu-MSFT answered

open external link from a site opened in a webview on xamarin

open my site through a webview, inside the page there are links with the tag <_blank>, but the application treats them as if they were not inside the webview (if it wants them to be pdf links). I tried to check around but I can't find anything that suits me. I saw that I can use the BrowserLaunchMode function, but I go back to the starting point because I don't know how to implement it in the webview and above all I don't know how to make the app recognize that I'm clicking a link. Could someone please tell me if there is a way to check when I touch the webview if I am clicking a link please?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

how to make the app recognize that I'm clicking a link. Could someone please tell me if there is a way to check when I touch the webview if I am clicking a link please?

Webview have a ShouldOverrideUrlLoading method of WebViewClient In the Android, if user click the hyper link, this method ShouldOverrideUrlLoading will be execute. You can use custom renderer for your webview like following code. You can get all of links by request.Url;

[assembly: ExportRenderer(typeof(WebView), typeof(MyWebviewRenderer))]
namespace ImageLongClickDemo.Droid
{
    class MyWebviewRenderer : WebViewRenderer
    {
        public MyWebviewRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {              
                Control.SetWebViewClient(new MyWebviewClient());
            }
        }
    }

    internal class MyWebviewClient : Android.Webkit.WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, Android.Webkit.IWebResourceRequest request)
        {
            var MyUrl=request.Url;
            Toast.MakeText(Android.App.Application.Context,MyUrl.ToString(),ToastLength.Short).Show();
            return base.ShouldOverrideUrlLoading(view, request);
        }
       
    }
}


Best Regards,

Leon Lu



If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.