9

If a page has a URL or a phone number on it that isn't a link is there any way to have WebView recognize it and automatically turn it into a link like you can with TextViews?

With a TextView you would simply set the android:autoLink to the desired settings:

<TextView
    android:autoLink="web|phone"
    ... />

but I can't find any equivalent for WebView.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143

2 Answers2

35

If you are loading your own (web) content from a String, then you can do something like this:

final String content = "My email is: firstname@email.com ...";
Spannable sp = new SpannableString(content);
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
myWebView.loadData(html, "text/html", "utf-8");
Harri
  • 828
  • 10
  • 13
  • 13
    Note that if the content string already contains HTML, you'll want to use `Spannable sp = new SpannableString( Html.fromHtml(content) )` so that your characters aren't entity-ized in `Html.toHtml(sp)`. – dvs Aug 16 '12 at 15:31
  • is it works for Lollipop version? I am running my app on both Lollipop and Kitkat but getting problem on Lollipop device? – Prashanth Debbadwar Jul 27 '15 at 10:41
  • 1
    This works but the problem with formatting. If content has tags that are not supported(e.g.
  • ) by webview.
  • – Hiren Dabhi Oct 20 '16 at 10:16