Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load local html file in webview android

I am trying to load the contents of a html file in a webview in android. However, it gives me the "Webpage not available error". If I try websites such as google or yahoo, they work.

The html file are under src > main > assests > index.html

Can someone help me with this issue. Thank You.

Following is my code :

setContentView(R.layout.activity_main);

WebView mWebView;
mWebView = (WebView) findViewById(R.id.activity_main_webview);

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

mWebView.loadUrl("file:///android_asset/index.html");

XML File :

<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
like image 721
user782400 Avatar asked Feb 04 '14 16:02

user782400


People also ask

How do I view local HTML files on Android?

You can use a local web server in your Android phone itself. There are many server apps out there in the Play Store, one such app is Simple HTTP Server. You can put your documents into the folder Android/data/com. android.

Does local storage work in WebView?

On Android, LocalStorage works well but only in the current webview. Multiple webviews of the same app can't share the same data with LocalStorage.

How do I enable JavaScript on Android WebView?

JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.


1 Answers

The html file should be placed in the assets folder (note the spelling), which will belong in the root directory of your project.

So move

src/main/assests/index.html

to

assets/index.html

In an Android Studio project use this folder:

/app/src/main/assets/index.html
like image 62
jimmithy Avatar answered Sep 22 '22 15:09

jimmithy