Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Webview Anchor Link (Jump link) not working

I have a WebView in my Android App that is loading an HTML string using the loadDataWithBaseURL() method. The problem is that local anchor links (<a href="#link">...) are not working correctly. When the link is clicked, it becomes highlighted, but does not scroll to the corresponding anchor.

This also does not work if I use the WebView's loadUrl() method to load a page that contains anchor links. However, if I load the same URL in the browser, the anchor links do work.

Is there any special handling required to get these to work for a WebView?

I am using API v4 (1.6).

There isn't much to the code, here are the relevant parts of some test code I've been working with:

WebView detailBody = (WebView) findViewById(R.id.article_detail_body); String s = "<a href=\"#link\">LINK!</a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!"; detailBody.loadDataWithBaseURL(API.HomeURL(this), s, "text/html", "utf-8", ""); 
like image 620
Joel Avatar asked Jun 14 '10 17:06

Joel


2 Answers

It looks like the problem is that I had a WebView within a ScrollView. The WebView isn't able to scroll to an anchor link when configured like this. After refactoring my layout to eliminate the ScrollView, the anchor links work correctly.

like image 105
Joel Avatar answered Sep 17 '22 19:09

Joel


Android Webview Anchor Link (Jump link) Not Working

True, WebView Anchor Links, or Jump Links initiated through the #LINK extension to the URL will not work when the WebView is inside of a ScrollView(*).

Still, the problem for me and apparently others is that the #LINK does work when launched from a touch in an href, but is ignored when launched via the URL. Other symptoms include navigating to the link only on the first time in a session or navigating to the bottom of the html file.

The Solution is to load the url after a short delay.

Here is an example:

My html is saved in assets: res/assets/help.html

With anchors like this:

<a name="helplinkcontacts"/> 

And loaded like this:

final String baseUrl = "file:///android_asset/help.html#helplinkcontacts"; final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text); helpTextView.loadUrl(baseUrl); // Ignores Anchor!! 

I added the timer like this:

final String baseUrl = "file:///android_asset/help.html#helplinkcontacts"; final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text); Timer timer = new Timer(); timer.schedule(new TimerTask() {     @Override     public void run() {         helpTextView.loadUrl(baseUrl);     } }, 400); 

Note: Shorter delays, such as 100ms failed to navigate to the link.

(*) It turns out that so many of us have our WebViews inside of ScrollViews because we started out with a TextView rendering Spannable text which both supports some HTML and requires a ScrollView. Anyways, remove the ScrollView as soon as you convert your TextView into a WebView.

like image 33
David Manpearl Avatar answered Sep 19 '22 19:09

David Manpearl