views:

95

answers:

2

Hi,

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", "");
A: 
try this

String myTemplate = "<a href=\"#link\">LINK!</a><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!";

myWebView.loadDataWithBaseURL(null, myTemplate, "text/html", "utf-8", null);

the word "Testing!" must be outside of the screen to see it works.

Jorgesys
Normal links work correctly. The problem I'm having is related to anchor jump links (see my test sample code).
Joel
I have many more <br/> tags in my test code to ensure that the anchor is well off the screen. I removed some of them to keep the code snippet from being absurdly wide. I apologize for any confusion.
Joel
don't worry Joel, but its working now right?
Jorgesys
No, it still is not working for me. Does this simple test code work for you?
Joel
yep it works!, could you post your xml layout?
Jorgesys
A: 

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.

Joel
ups! =), in a webview the scroll must be automatically! so we don't need nest into a Scrollview good spot.
Jorgesys