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

Anonymous
2021-10-31T07:59:04.203+00:00

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?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,416 Reputation points Microsoft Vendor
    2021-11-01T07:10:35.47+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful