user3619542 user3619542 - 3 months ago 10
Android Question

How to load a webpage offline in android

Im trying to load terms and conditions of an app I am working on in my application. The client has provided me an html file of the terms and condition and I do not have any kind of server access. So technically the app needs to be carry that file with it. I have done web views where online data is shown in web view but I do not have any ice for this situation. I know I am missing a stupid point here. Please help

My layout file is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:orientation="vertical" >

<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</LinearLayout>

Answer

Any document to be used on the file is generally placed in the assets folder. Refer to this documentation. Here is a working snippet.

try {
            WebView mWebView = null;
            mWebView = (WebView) findViewById(R.id.webview);
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl("file:///android_asset/index.html"); 
        } catch (Exception e) {
            // TODO: handle exception
            this.finish();
        }
Comments