Danish Butt Danish Butt - 5 months ago 8
Javascript Question

webview cannot load index.html

Hello everyone i have an issue , i place all the file like html, css, js in assets folder but it did not show me on a webview.
Check the images.
Index.html file

And this this the assets folder Assets

Here is the code in the MainActivity

public class MainActivity extends AppCompatActivity {

WebView webView;
WebSettings webSettings;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView=(WebView)findViewById(R.id.webView);
assert webView != null;
webView.loadDataWithBaseURL("file:///assets/index.html","text/html","utf-8",null,null);

webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

////////////////////////////////

}
}

Answer

Searching online it looks like you need to use android_asset instead of assets in the path url. Also, it looks like your index.html is under another folder called themes so the correct url would be "file:///android_asset/themes/index.html". If I was just looking at that image incorrectly then the correct url is "file:///android_asset/index.html". Also, use webView.loadUrl("file:///android_asset/index.html"); instead because you are not using the other method correctly. Documentation