1

Lets take a look at this for example: Loading existing .html file with android WebView I load my HTML file but it contains references to other local css and js files. Do I need use the same file:/// URL inside the HTML file?

Community
  • 1
  • 1
zisis
  • 35
  • 6

2 Answers2

0

No, your structure should be like this:

/assets/html/index.html

/assets/scripts/index.js

/assets/css/index.css

Then just do ( Android WebView: handling orientation changes )

    //in onCreate() for Activity, or in onCreateView() for Fragment
    if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more
        webView.loadUrl("file:///android_asset/html/index.html");
    } else {
        webView.restoreState(WebViewStateHolder.INSTANCE.getBundle());
    }

Make sure you add

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
    }

Then just use urls

<html>
<head>
    <meta charset="utf-8">
    <title>Zzzz</title>
    <script src="../scripts/index.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/index.css">

EDIT:

That particular code will work only if you add the code that I linked

public class WebViewFragment extends Fragment { //could be activity
    private enum WebViewStateHolder
    {
        INSTANCE;

        private Bundle bundle;

        public void saveWebViewState(WebView webView)
        {
            bundle = new Bundle();
            webView.saveState(bundle);
        }

        public Bundle getBundle()
        {
            return bundle;
        }
    }

    @Override
    public void onPause()
    {
        WebViewStateHolder.INSTANCE.saveWebViewState(myWebView);
        super.onPause();
    }

As for whatever "security issue" you speak of, you didn't share enough data.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • if(WebViewStateHolder.INSTANCE.getBundle() == null) { //this works only on single instance of webview, use a map with TAG if you need more webView.loadUrl("file:///android_asset/html/index.html"); } else { webView.restoreState(WebViewStateHolder.INSTANCE.getBundle()); } doesnt work and there seems to be a security issue with the reference inside the script. (cannot load file) – zisis Apr 16 '15 at 18:48
  • `there seems to be a security issue with the reference inside the script. (cannot load file)` I don't know what you mean. – EpicPandaForce Apr 16 '15 at 18:52
  • It says file does not exist(the one in the reference) – zisis Apr 16 '15 at 21:12
  • then you should put it there, because apparently it's not there! I got this code from existing working code, therefore you're structuring something wrong. Is it in the `src\main\assets` folder? – EpicPandaForce Apr 16 '15 at 21:52
0

According to Rendering HTML in a WebView with custom CSS, you dont have to specify each CSS or JS. Include them in the HTML file itself.

Community
  • 1
  • 1