views:

533

answers:

2

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.

+2  A: 

I don't know about any way which would make this work just by changing a setting, but a workaround would be to wait until the web page finishes loading and then do:

yourWebView.loadUrl("javascript:(function(){ /* code that creates links */ })()");

This will inject javaScript into the already loaded web page. There's a slightly longer example available here: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.

You can find the JavaScript source for creating links if you take a look at the source of Linkify script for Greasemonkey (it's a plugin for Firefox in case you're not familiar with it). I believe it comes with the default install.

Aleksander Kmetec
Ha... that is exactly the route I was planning (including using Linkify as an example) to take if there wasn't a built in way to do this. Thanks!
fiXedd
Doesn't the Browser have auto-detection for phone numbers and addresses, though? There's got to be a simpler way. I just don't know it.
Klondike
It does have some sort of address (and possible phone number) detection, yes. But from my experience it's not very reliable (last time I tested it it only recognized US addresses) and it also doesn't highlight what it detects.
Aleksander Kmetec
The Linkify greasemonkey script didn't end up working because (apparently) WebView doesn't have XPath. I ended up digging up a bookmarklet that used DOM and (after modification) it worked fine.
fiXedd
A: 

@fiXedd I'm looking to do the same thing, any chance you'd consider sharing your JS that you used?

This should be a comment, not an answer, but sure. [Here](http://pastie.org/1123746)'s the JS I used and [here](http://pastie.org/1123747)'s the external methods to handle the links. The accepted answer should explain how to glue them together.
fiXedd
fiXedd - I'm aware it should be a comment, but I'm a new user and can't yet comment on things ;) Thanks for that mate.