Preventing autolinking of emails and URLs in an Android WebView

后端 未结 5 1793
温柔的废话
温柔的废话 2021-02-10 00:48

I have a WebView which may contain data that appears to be getting \"auto linked\". Something that looks like an email address is becoming clickable, even though i

相关标签:
5条回答
  • 2021-02-10 00:57

    I had the same problem, tried this:

    <a onClick=\"return false;\">jorgesys@elnorte.com</a>
    

    it did not worked.

    Then tried this:

    <a href='javascript:void(0);'>800-644-9737</a>
    

    and it did the trick

    0 讨论(0)
  • 2021-02-10 00:58

    I know this is a bit late, but for future reference, this might be a solution that will work regardless if the links are auto created or defined in the <a>-tag.

    myWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // return true; // will disable all links
    
            // disable phone and email links
            if(url.startsWith("mailto") || url.startsWith("tel")) {
                return true;
            }
    
            // leave the decision to the webview
            return false;
        }
    });
    
    0 讨论(0)
  • 2021-02-10 01:02

    Hi Squeaggy why you do want to eliminate that funcionality from the webview, but well a tricky way would be including onClick="return false;" in the anchor tag that contains the email or URL.

    <a onClick=\"return false;\">jorgesys@elnorte.com</a>
    
    0 讨论(0)
  • 2021-02-10 01:08

    That appears to be unchangeable functionality of the WebView.

    You could do the opposite of this Is there any way to have WebView auto-link URLs and phone numbers in Android? and create a javascript link stripper (instead of the proposed link injector there).

    Not sure what else would work for this.

    0 讨论(0)
  • 2021-02-10 01:15

    To do all the email addresses, add a meta tag:

    <meta name="format-detection" content="email=no" />
    

    You can also disable physical address and telephone detection:

    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="address=no" />
    

    In my own application, though, I needed PhD's solution to prevent only one email from being linked.

    0 讨论(0)
提交回复
热议问题