How to set mailto tag binding: attribute in webview, android

I tried to set the anchor tag tag attribute as

<a href='mailto:info@company.com'>info@company.com</a>

in web view. when I run the application on the simulator and click on the link, it shows "Unsupported action .."

How can I set the mailto attribute in android web browser ...

thank

+3
source share
3 answers

WebView does not support advanced HTML tags ... what you need to do:

  • Install the web client in your webview and redefine the download URL.
  • When you find the link with mailto, try sending an email.

, . , , :

public void onCreate(Bundle icicle) {
    // blablabla
    WebView webview = (WebView) findViewById(R.id.webview); 
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient( new YourWebClient()); 
    // blablabla
}

private class YourWebClient extends WebViewClient {     
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("mailto")) {
            // TODO: extract the email... that your work, LOL
            String email = "";
            sendEmail();
            return super.shouldOverrideUrlLoading(view, url);
        }
        view.loadUrl(url);
        return true;
    }
}

:

public void sendEmail(String email){
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});

    String mySubject = "this is just if you want";
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySubject);
    String myBodyText = "this is just if you want";
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, myBodyText);
    context.startActivity(Intent.createChooser(intent, "Send mail...));
}
+3

myTemplate ="<a>info@company.com</a>";

myTemplate ="info@company.com";

WebView

mWebView.loadDataWithBaseURL(null, myTemplate, "text/html", "utf-8", null);
0

Here is another Cristian answer.

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
  if (!(url.startsWith("http") || url.startsWith("#"))) {
    launchIntent(url);
    return true;
  }
  view.loadUrl(url);
  return true;
}

private void launchIntent(String url){
    final Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
    Uri uri = Uri.parse(url);
    intent.setData(uri);
    context.startActivity(intent);
}

This will allow you to bind tags such as mailto: tel: and google.navigation: q = + url + uncoded + address

You can set up a conditional if your html page has other bindings that you do not want to run with.

0
source

Source: https://habr.com/ru/post/1753566/


All Articles