Using loadDataWithBaseURL disables links in webview
Asked Answered
B

3

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.

Bedstead answered 23/7, 2019 at 13:24 Comment(0)
B
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.

Bedstead answered 26/10, 2021 at 9:8 Comment(0)
D
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) {
    }
}
Driedup answered 1/8, 2019 at 9:49 Comment(11)
May I ask you if you have tried it? for I'm already using all of the above.Bedstead
yes, it's working for me, is your base url same for your href url?Driedup
shouldOverrideUrlLoading is not even getting called, and no href is not same as the url passed to loadDataWithBaseURLBedstead
would you kindly post the code you are using for loadDataWithBaseURLBedstead
same code I am using. can I know what is your base url & href url that you load on anchor tags click?Driedup
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 youtube.com/utubeLinkBedstead
what's in templateString?Driedup
have you try this webview.loadUrl(itemURL);? it works for me to open youTube urlDriedup
templateString is to manipulate the content of the html this is why it's essential, in order to add paging and some other customizationsBedstead
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.Bedstead
can you please make a video on your issue, I want to know what exactly it isDriedup
R
0

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

 webview.loadDataWithBaseURL(null, htmltext, "text/html", "utf-8", null);
Rimrock answered 28/7, 2019 at 2:30 Comment(2)
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.Bedstead
Could you check this #11188848Rimrock
B
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.

Bedstead answered 26/10, 2021 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.