I think that it is not a rare case, but it is a memorandum when you want to hook the tap of the anchor link (link in the page) on the web screen with Android WebView and make something work on the application side at that timing.
** Example: If you include WebView in ScrollView, the anchor link will not work, so I want to hook the tap of the anchor link on the application side to scroll the ScrollView **
When you want to scroll the WebView and the native View together, you may implement something like the above
At first, what should I do when I see the Url in WebViewClient's shouldOverrideUrlLoading and it is an in-page link! I was thinking, but shouldOverrideUrlLoading is not called for links within the same page ...
ShouldOverrideUrlLoading is not called when tapping an in-page link, but onPageFinished is. You can also take a Url as an argument, so what should I do here? For example, suppose the in-page link looks like this:
<a href="#top">Back to top</a>
It is OK if you write as follows using WebViewClient
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if(url.contains("#top")) {
/**Write what you want to do here**/
}
}
});
that's all.
Recommended Posts