0

Hi I have a WebView where I load some html text using this code:

        text6.loadDataWithBaseURL("","<html>\n" +
            "<head>\n" +
            "<style type=\"text/css\">\n" +
            "@font-face {\n" +
            "    font-family: MyFont;\n" +
            "    src: url(\"file:///android_asset/fonts/Brandon_light.otf\")\n" +
            "}\n" +
            "body {\n" +
            "    font-family: MyFont;\n" +
            "    font-size: medium;\n" +
            "    text-align: center;\n" +
            MessageFormat.format("link: {0};color:{1}", ColorManager.generalColorHEX, ColorManager.colorNameActivityCellHEX)+
            "}\n" +
            "</style>\n" +
            "</head>\n" +
            MessageFormat.format("<body link={0}>", ColorManager.generalColorHEX) +
            MessageFormat.format("{0}", DataManager.surveyDesc) +
            "</body>\n" +
            "</html>",DataManager.mimeType, DataManager.encoding, "");

the text that is loaded could contains email address, phone numbers and links. Is it possibile that the WebView recognises the hyperlinks and on tap it executes the correct actions (send email, open links, call a number...) thanks!

1 Answers1

0

You could override the url with something like.

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;
}

same for mail etc, just add the url thats required.

Raymie
  • 109
  • 1
  • 9
  • Hi, the problem is that, for example, the phone number does not start with “tel:” but it’s just a number – Francesco Bordignon Aug 21 '18 at 06:19
  • Well the answer i gave answered your question as you said the number was already a hyperlink. If you have access to the original data just append the appropriate uri to the string. If you don't you'd have to do some string / pattern matching to detect telephone number then make it a hyperlink. – Raymie Aug 21 '18 at 11:57
  • Sorry I made a mistake the number is not a hyper like, how can I make it a hyperlink? – Francesco Bordignon Aug 21 '18 at 14:07
  • Maybe best asking a new question or editing the title of this. I'm not entirely sure of the best way to do it, personally i would probably use regex against the string and parse for email , phone patterns etc . – Raymie Aug 21 '18 at 18:12
  • https://stackoverflow.com/questions/16053797/regex-to-find-email-address-from-a-string – Raymie Aug 21 '18 at 18:13
  • String str = "This is 1 test 123-456-7890"; Pattern pattern = Pattern.compile("\\d{3}-\\d{3}-\\d{4}"); Matcher matcher = pattern.matcher(str); if (matcher.find()) { System.out.println(matcher.group(0)); } . Then just append the uri to the string. – Raymie Aug 21 '18 at 18:14