On the android, when the built in browser comes across an email or a phone number and it is pressed, a dialog box comes up for each.
However, if i navigate to that url inside my Application's webview, this no longer is the case.
Is there anyway for my webview to exhibit the same properties as the browser application in detecting emails and phone numbers and popping up the respective default dialogs when pressed?
I know that the iPhone can do this with a checkbox in the Interface Builder very easily, I am hoping the android can do the same.
I have taken a look into such links as:
Is there any way to have WebView auto-link URLs and phone numbers in Android?
and using the function that interrupts the link click but that doesn't really help me because phone numbers and emails don't necessarily have to be linked.
This is for phone link, you can add another if to mail link :)
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
I have set the WebView to detect phone, email and address (in which case would go to Google Maps). This is what I have:
URL = "file:///android_asset/dir/people.html";
webView = (WebView) findViewById(R.id.webViewDir);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
} else if (url.startsWith("mailto:")) {
url = url.substring(7);
String body = "Body of message.";
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("application/octet-stream");
mail.putExtra(Intent.EXTRA_EMAIL, new String[] { url });
mail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
mail.putExtra(Intent.EXTRA_TEXT, body);
startActivity(mail);
return true;
} else if (url.startsWith("map:")){
url = url.substring(4);
String map = "http://maps.google.com/maps?q=" + url;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(map));
startActivity(intent);
return true;
}
return false;
}
});
webView.loadUrl(URL);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With