Working with Android-Studio, I am trying to use WebView to load a local html file that contains javascript.
First, I created an assets
folder inside the Java
folder. Then, I created two folders inside assets folder: html
and scripts
. I put my index.html
page inside html folder and the index.js
file inside scripts folder. The two paths look like the following in Android-Studio:
- app/java/assets/html/index.html
- app/java/assets/scripts/index.js
Now, when I try to load the index.html, I use the following syntax:
myWebView.loadUrl("file:///assets/html/index.html");
but it is not working. I also used each of the following:
myWebView.loadUrl("file://assets/html/index.html");
myWebView.loadUrl("file://android_assets/html/index.html");
myWebView.loadUrl("file:///android_assets/html/index.html");
Still, when the page loads, it shows "Webpage not available"!!
The app works fine, the intent start and a webpage loads with the above warning message.
- What is right syntax to load the webpage from a local-created folder ?
- What is the right syntax for loading index.js inside the index.html ?
Here is the code:
public class DisplayMessageActivity extends Activity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_assets/html/index.html"); // doesn't work
// myWebView.loadUrl(""); // works
}
UPDATE
Here is the index.html
:
<html>
<header><title>This is title</title></header>
<body>
Hello world
</body>
</html>
No javascript at the moment.
Working with Android-Studio, I am trying to use WebView to load a local html file that contains javascript.
First, I created an assets
folder inside the Java
folder. Then, I created two folders inside assets folder: html
and scripts
. I put my index.html
page inside html folder and the index.js
file inside scripts folder. The two paths look like the following in Android-Studio:
- app/java/assets/html/index.html
- app/java/assets/scripts/index.js
Now, when I try to load the index.html, I use the following syntax:
myWebView.loadUrl("file:///assets/html/index.html");
but it is not working. I also used each of the following:
myWebView.loadUrl("file://assets/html/index.html");
myWebView.loadUrl("file://android_assets/html/index.html");
myWebView.loadUrl("file:///android_assets/html/index.html");
Still, when the page loads, it shows "Webpage not available"!!
The app works fine, the intent start and a webpage loads with the above warning message.
- What is right syntax to load the webpage from a local-created folder ?
- What is the right syntax for loading index.js inside the index.html ?
Here is the code:
public class DisplayMessageActivity extends Activity {
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_assets/html/index.html"); // doesn't work
// myWebView.loadUrl("http://www.example."); // works
}
UPDATE
Here is the index.html
:
<html>
<header><title>This is title</title></header>
<body>
Hello world
</body>
</html>
No javascript at the moment.
Share Improve this question edited Sep 9, 2016 at 11:22 McLan asked Sep 9, 2016 at 10:35 McLanMcLan 2,6989 gold badges55 silver badges91 bronze badges 7- 1 please show content of your index.html. You might have incorrect reference to index.js. Please use this format mWebView.loadUrl("file:///android_asset/html/index.html"); To isolate issue with index.js. Please create a hello_world.html and try loading in html to see if that's loaded correctly – Amod Gokhale Commented Sep 9, 2016 at 10:43
- @AmodGokhale : I am already using a raw hello_world html file (please check the update). At the moment, I am just trying to load the correct page. But it is not working !! – McLan Commented Sep 9, 2016 at 10:53
- 1 never mind.. for android studio file should be located @ /app/src/main/assets/html/index.html and then load using file:///android_asset/html/index.html – Amod Gokhale Commented Sep 9, 2016 at 10:58
- @AmodGokhale .. No error at the logcat .. – McLan Commented Sep 9, 2016 at 11:02
- 1 no.. you have created assets folder inside java.. it should be /src/main/assets/... file name.. your path is app/java/assets – Amod Gokhale Commented Sep 9, 2016 at 11:06
2 Answers
Reset to default 4Create assets folder under main ( not inside java ) and then use
myWebView.loadUrl("file:///android_asset/html/index.html");
not
myWebView.loadUrl("file:///android_asset**s**/html/index.html");
Use above answer just replace
webView.loadUrl("file://android_asset/index.html");
with webView.loadUrl("file:///android_asset/index.html");