Load local html file in webview on android

Loading Html File from Assets folder in Android WebView with loading local HTML webpage inside APK.

Android assets folder is used to store external files like images, text files, html files directly into android application so whenever android apps runs it will automatically loads that particular calling file into the view you set. Android developer can load html( Hyper text mark-up language ) files through assets folder all you have to do is put designed html file inside assets folder and set into webview. So here is the complete step by step tutorial for Load local html file in webview on android.

android-project-download-code-button

How to Load local html file in webview on android.

Create HTML file and put that file into assets folder.

assets folder android

Code for MainActivity.java file.

package com.webview_loadhtml_page_from_accets_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class MainActivity extends Activity {

 WebView webView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 webView = (WebView)findViewById(R.id.webView1);
 
 WebSettings webSetting = webView.getSettings();
 webSetting.setBuiltInZoomControls(true);
 webSetting.setJavaScriptEnabled(true);
 
 webView.setWebViewClient(new WebViewClient());
 webView.loadUrl("file:///android_asset/first.html");
 }

 private class WebViewClient extends android.webkit.WebViewClient
 {
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) 
 {
 return super.shouldOverrideUrlLoading(view, url);
 }
 }
 
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 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"
 tools:context="com.webview_loadhtml_page_from_accets_android_examples.com.MainActivity" >

 <WebView
 android:id="@+id/webView1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

</RelativeLayout>

Code for first.html file.

<html>
<head></head>
<body>

<h1> this is text through assets folder </h1></br>
<h2> h2 text</h1>

</body>
</html>

Screenshot:

Load local html file in webview on android

Click Here to download Load local html file in webview on android project.

One Comment

  1. thanks…………
    good idea to use html5 in android.

Leave a Reply

Your email address will not be published. Required fields are marked *