In this example we
learn, how to load simple webpage from asset folder to webview, like
display offline web page into android webview.
CREATE NEW ANDROID PROJECT:
Step 1: Write code into notepad and save as 'all file' with name "help.html" and copy into " ...asset "
folder.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>Welcome</title>
</head>
<body>
<h1>Welcome</h1>
</body>
</html>
Step 2: Write code into activity_main.xml
<?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>
Step 3: Write code into MainActivity.java
package dev.androidapplink.webviewapp;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView myBrowser;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Lunch when start Activity
// web common settings
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser = (WebView)findViewById(R.id.mybrowser);
//give path of html page
myBrowser.loadUrl("file:///android_asset/help.html");
}
}
Step 4: Run your App:
CREATE NEW ANDROID PROJECT:
Step 1: Write code into notepad and save as 'all file' with name "help.html" and copy into " ...asset "
folder.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width; user-scalable=0;" />
<title>Welcome</title>
</head>
<body>
<h1>Welcome</h1>
</body>
</html>
Step 2: Write code into activity_main.xml
<?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>
Step 3: Write code into MainActivity.java
package dev.androidapplink.webviewapp;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView myBrowser;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Lunch when start Activity
// web common settings
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser = (WebView)findViewById(R.id.mybrowser);
//give path of html page
myBrowser.loadUrl("file:///android_asset/help.html");
}
}
Step 4: Run your App:
No comments :
Post a Comment