2

Many of the answers I've been seeing here are 6+ years old so I was wondering if I could get a more recent answer as they haven't been working for me. I have an html file file.html which contains a

<LINK href="stylesheet.css" type="text/css" rel="stylesheet">

for the CSS and the following for the javascript:

<script src="myscript.js"></script>

file.html, stylesheet.css and myscript.js are all stored in app/assets/Content/

So I am trying to run it with:

WebView webView = (WebView) findViewById(R.id.event_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///assets/Content/file.html");

However, the WebView is showing the error:

Webpage not available: The webpage at file:///assets/Content/file.html could not be loaded because net::ERR_FILE_NOT_FOUND

I have also considered using loadDataWithBaseURL but this requires the data field to contain all of the html which is too big of a file to hardcode. Does anybody know how I could use html+css+javascript for a WebView?

0

1 Answer 1

1
+50

Your URI for asset folder is incorrect. Change asset to android_asset. As follows:

webView.loadUrl("file:///android_asset/file.html");

Sign up to request clarification or add additional context in comments.

3 Comments

So the directory structure can stay the same, /ap/android/assets/Content/file.html but the line in Java will be webView.loadUrl("file:///android_asset/Content/event_progress_thermometer.html");? EDIT: Yep, this worked!
How would I then call a function in my javascript file myscript.js from java?
You can use webView.evaluateJavascript("<functionname>()", null) to call JS method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.