Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Android WebView detect and popup email dialog or phone call dialog

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.

like image 483
changus Avatar asked Aug 03 '10 21:08

changus


2 Answers

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;
    }
like image 164
Jayyrus Avatar answered Oct 18 '22 14:10

Jayyrus


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);
like image 31
Gorby Oz Avatar answered Oct 18 '22 14:10

Gorby Oz