Android Local HTML
assets
Easiest way to read local HTML file is to save file into assets folder.
By default, Android Studio project doesn’t have assets directory.
You should make under app/src/main
And save html file under it.
Source code
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="jp.co.rakuten.toolbar.webviewtest.MainActivity"
tools:showIn="@layout/activity_main">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</RelativeLayout>
Source
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
//webView.getSettings().setJavaScriptEnabled(true);
// Load local webview
webView.loadUrl("file:///android_asset/index.html");
}
HTML file
<html>
<head>
<title>JavaScript</title>
<script>
function test() {
alert("OK");
}
</script>
</head>
<body>
<button onclick="test();">Click me!</button>
</body>
</html>
