Alex Rivera | Logout

Android Webview Anchor Link (Jump link) not working

Asked 2010-06-14T17:50:56.040
34

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", "");
Edit
Report

1 Answer

4

I had a similar problem. Nothing would jump to anchor tags in the html. I didn't have my WebView within a ScrollView. Instead the problem was the base url I passed into loadDataWithBaseURL did not have a colon (':') in it. I believe the baseUrl needs to have some text, then a colon, then some more text, for example "app:htmlPage24".

So here's the initial call to my WebView, just to load the data in the string HTML_24:

wv.loadDataWithBaseURL("app:htmlPage24", HTML_24, "text/html", "utf-8", null);

Then I have a list that jumps to sections on the screen, depending on the list item you tap:

sectionsLV.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
    {
        wv.loadUrl("app:htmlPage24#section" + arg2);
    }
});

HTML_24 is something like:

<html>
...
<a name="section1"/>

...
<a name="section2"/>

...
<a name="section3"/>

...
</html>
answered 2011-08-31T22:08:35.047

Your Answer