-1

In my application (target 4.0), I have a chunk of HTML (which was pulled and parsed from a webpage) that I am displaying in a TextView. This HTML contains a large number of links (up to 100), most of which are relative, but some are absolute. I want the user to be able to click these relative urls URLs to launch a web browser.

How can I do this without going through the HTML and changing the URLs? Can I tap into a link click event?

2 Answers2

0

You have to set a WebViewClient to your WebView and override shouldOverrideUrlLoading (and perhaps onLoadResource too).

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • This is inside a TextView, but if I can't find a way to do it there maybe I'll just use a WebView. Thanks! – Matthew Dunlap Aug 31 '14 at 06:43
  • I saw this and thought it wouldn't work, but reading over it again it seems I may be able to capture all the link clicks, and then launch an activity which decides how to launch into a web browser. Will try tomorrow. – Matthew Dunlap Aug 31 '14 at 06:50
  • It made the most sense to just use a WebView and override shouldOverrideUrlLoading, launching an ACTION_VIEW intent on click. I did this in conjunction with WebView's loadDataWithBaseURL, setting the BaseUrl to the url needed. – Matthew Dunlap Sep 01 '14 at 03:27
0

You can match URL (and other patterns) using Linkifyand you can parse your content to use <a> tags like this:

Android Make hyperlinks in textview clickable

In other words, you need the href to contain a fully qualified URL, but you can still display the relative URL to the user.

No client URL works without full qualification, so you need to give the client a context. But this allows you to display relative URL's to the user.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36