5

I use the following code to load html content of ebooks where templateString contains the html content which connects to stylesheet and images in the main file.

String itemURL = "file://" + itemPath;
testWV.loadDataWithBaseURL(itemURL,  templateString, "text/html", "UTF-8", "about:blank");

The problem I'm facing is that anchor links are not responsive at all.

I noticed if itemURL was null or if I use loadData instead of loadDataWithBaseURL, the links work but I loose the connection of the images and the styling connected through the itemURL.

Kindly note that the webview visibility is always set to visible. Add I have added the following features to the webview

this.getSettings().setJavaScriptEnabled(true);
this.requestFocusFromTouch();
this.setVerticalScrollBarEnabled(false);
this.setHorizontalScrollBarEnabled(false);
this.getSettings().setSupportZoom(true);
this.getSettings().setBuiltInZoomControls(true);
this.getSettings().setDisplayZoomControls(false);
this.getSettings().setAllowContentAccess(true);
this.getSettings().setAllowFileAccess(true);
this.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.getSettings().setAllowFileAccessFromFileURLs(true);
this.getSettings().setAllowUniversalAccessFromFileURLs(true);

This is the onTouch method initialized for the webview:

this.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {

        WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
        System.out.println("getExtra: "+hr.getExtra());
        // getExtra always gives null when webview loaded with loadDataWithBaseURL while it should give the url of the link pressed when the user touches a link

        return false;
    }
});

If further code is needed, I can share it.

coder
  • 5,200
  • 3
  • 20
  • 45

3 Answers3

1

set WebViewClient to your webView and load data in loadDataWithBaseURL and pass your base url

this will helps to load anchor url into webview

 webview.getSettings().setJavaScriptEnabled(true);
 webview.requestFocusFromTouch();
 webview.setWebViewClient(new MyWebClient());

here is WebViewClient class

class MyWebClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    public void onPageFinished(WebView view, final String url) {
    }
}
Priyanka
  • 3,369
  • 1
  • 10
  • 33
  • May I ask you if you have tried it? for I'm already using all of the above. – coder Aug 01 '19 at 10:08
  • yes, it's working for me, is your base url same for your `href` url? – Priyanka Aug 01 '19 at 11:27
  • shouldOverrideUrlLoading is not even getting called, and no href is not same as the url passed to loadDataWithBaseURL – coder Aug 01 '19 at 12:01
  • would you kindly post the code you are using for loadDataWithBaseURL – coder Aug 01 '19 at 12:03
  • same code I am using. can I know what is your base url & href url that you load on anchor tags click? – Priyanka Aug 01 '19 at 12:05
  • The base url that I use for loadDataWithBaseURL is the path to its location: file:///data/user/0/com.app/files/BookUnZipDir/titlepage.xhtml as for href https://www.youtube.com/utubeLink – coder Aug 01 '19 at 12:28
  • what's in `templateString`? – Priyanka Aug 01 '19 at 12:33
  • have you try this `webview.loadUrl(itemURL);`? it works for me to open youTube url – Priyanka Aug 01 '19 at 12:47
  • templateString is to manipulate the content of the html this is why it's essential, in order to add paging and some other customizations – coder Aug 02 '19 at 03:58
  • Kindly note if you help through this even if the bounty is over I would put another one, but it's crucial for me to solve it. – coder Aug 02 '19 at 03:59
  • can you please make a video on your issue, I want to know what exactly it is – Priyanka Aug 02 '19 at 04:11
0

Can you try to load HTML text instead of giving URL as below:-

 webview.loadDataWithBaseURL(null, htmltext, "text/html", "utf-8", null);
Shalu T D
  • 3,921
  • 2
  • 26
  • 37
  • Thank you for your contribution, as I have mentioned in my answer I have given it a try and it would actually work making the text selectable. but in the absence of the URL, the webview can no longer connect to pictures and style sheet of the html which are located through the URL. – coder Jul 29 '19 at 06:47
  • Could you check this https://stackoverflow.com/questions/11188348/android-html-anchor-link-works-only-once-in-webview/15162998#15162998 – Shalu T D Jul 29 '19 at 07:20
0

Seems the issue was not related to baseURL but to the css file connected to the WebView. In the absence of the baseURL, the css file was not loaded which enabled the footnotes to be clickable.

the line of code in the css file that was causing the issue was:

pointer-events: none;

So if anyone is looking for a way to make the webview non interactive, this is one way of doing so.

Thanks for every person that has contributed to this question.

coder
  • 5,200
  • 3
  • 20
  • 45