handling links in a webview

I assume you are already overriding shouldOverrideUrlLoading, you just need to handle this special case.

mWebClient = new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("mailto:")){
                MailTo mt = MailTo.parse(url);
                Intent i = newEmailIntent(MyActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
                startActivity(i);
                view.reload();
                return true;
            }

                else{
                    view.loadUrl(url);
                }
                return true;
            }
       };
    mWebView.setWebViewClient(mWebClient);

    public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
      intent.putExtra(Intent.EXTRA_TEXT, body);
      intent.putExtra(Intent.EXTRA_SUBJECT, subject);
      intent.putExtra(Intent.EXTRA_CC, cc);
      intent.setType("message/rfc822");
      return intent;
}

Leave a Comment