Skip to content Skip to sidebar Skip to footer

Webview Email Link (mailto)

I have a view and view the site has malito code to send email. When I open the link opens in an error. I want that when I open the link opens Gmail app or another email applicati

Solution 1:

You have to create a subclass of WebViewClient and override mailto URL loading. Example:

publicclassMyWebViewClientextendsWebViewClient {
  privatefinal WeakReference<Activity> mActivityRef;

  publicMyWebViewClient(Activity activity) {
    mActivityRef = newWeakReference<Activity>(activity);
  }

  @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {
      finalActivityactivity= mActivityRef.get();
      if (activity != null) {
        MailTomt= MailTo.parse(url);
        Intenti= newEmailIntent(activity, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
        activity.startActivity(i);
        view.reload();
        returntrue;
      }
    } else {
      view.loadUrl(url);
    }
    returntrue;
  }

  private Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
    Intentintent=newIntent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_EMAIL, newString[] { 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;
  }
}

Then you have to set this custom WebViewClient to your WabView:

webView.setWebViewClient(newMyWebViewClient(activity);

Solution 2:

You should update your's WebViewClient with the following:

@SuppressWarnings("deprecation") 
@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
    proceedUrl(view, Uri.parse(url))
    returntrue; 
} 

@TargetApi(Build.VERSION_CODES.N)
@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    proceedUrl(view, request.getUrl());
    returntrue; 
}

privatevoidproceedUrl(View view, Uri uri){  
    if (uri.toString().startsWith("mailto:")) {
        startActivity(newIntent(Intent.ACTION_SENDTO, uri));
    } elseif (uri.toString().startsWith("tel:")) {
        startActivity(newIntent(Intent.ACTION_DIAL, uri));
    } else { 
        view.loadUrl(uri.toString());
    } 
} 

Solution 3:

Note : - After Android Nougat shouldOverrideUrlLoading is Deprecated

You need to use shouldOverrideUrlLoading along with shouldOverrideUrlLoading for better support.

Also, you might want to check if URL have mailto: or tel:, which are used in HTML5 to trigger mail client and phone dial respectively.

A complete solution will look like this now

@SuppressWarnings("deprecation")@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {  
        //Handle mail Urls 
        startActivity(newIntent(Intent.ACTION_SENDTO, Uri.parse(url)));
    } elseif (url.startsWith("tel:")) {
        //Handle telephony Urls 
        startActivity(newIntent(Intent.ACTION_DIAL, Uri.parse(url)));
    } else { 
        view.loadUrl(url);
    } 
    returntrue; 
} 

@TargetApi(Build.VERSION_CODES.N)@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    finalUriuri= request.getUrl();
    if (uri.toString().startsWith("mailto:")) {
        //Handle mail Urls 
        startActivity(newIntent(Intent.ACTION_SENDTO, uri));
    } elseif (uri.toString().startsWith("tel:")) {
        //Handle telephony Urls 
        startActivity(newIntent(Intent.ACTION_DIAL, uri));
    } else { 
        //Handle Web Urls 
        view.loadUrl(uri.toString());
    } 
    returntrue; 
} 

Post a Comment for "Webview Email Link (mailto)"