Thursday, 20 October 2011

Load local HTML webpage inside APK

Using WebView, your app can load local HTML file (inside your APK).

Load local HTML webpage inside APK

Create your own HTML file under /assets/ folder.

/assets/mypage.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>My HTML</title>
</head>
<body>
<h1>MyHTML</h1>
</body>
</html>


Modify main.xml to add a WebView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<WebView
android:id="@+id/mybrowser"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>


Java code
package com.exercise.AndroidHTML;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class AndroidHTMLActivity extends Activity {

WebView myBrowser;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBrowser = (WebView)findViewById(R.id.mybrowser);

myBrowser.loadUrl("file:///android_asset/mypage.html");

}
}


Download the files.

Related article:
- AndroidBrowser, implement a Android Browser using WebView
- Run Android Java code from Webpage



No comments:

Post a Comment