Paradox
Paradox

Reputation: 4556

Android WebView with css and javascript

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?

Upvotes: 2

Views: 1175

Answers (1)

Sagar
Sagar

Reputation: 24907

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

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

Upvotes: 1

Related Questions