Search code examples
androidhtmlandroid-webviewmailto

How to handle mailto[HTML] in android webview


I have in a HTML page a href link where I link to email adress like this:

<p>sometext <a href="mailto:[email protected]">[email protected]</a></p>

This is my android MainActivity:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient;

import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends Activity {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;
private WebView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view = (WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.getSettings().setDomStorageEnabled(true);
    view.setWebViewClient(new MyBrowser());
    view.loadUrl("file:///android_asset/www/index.html"); //try js alert
    view.setWebChromeClient(new WebChromeClient()); // adding js alert 


}

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
        view.goBack(); //method goback()
        return true;
    }

    return super.onKeyDown(keyCode, event);
    }
}

I know I need to do something with the main activity but I don't know what? So can somone help me out?

Thanks in advance!


Solution

  • Inside shouldOverrideUrlLoading() method used this logic

      if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
      else {
             view.loadUrl(url);
           }