Loading Html File from Assets folder in Android WebView

Loading Html File from Assets folder in Android WebView

In this tutorial we are going to learn how to load html file in android WebView control. Android WebView is a View control that is use to display html web pages in android application.

It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.

According android developers guide – WebView by default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored. If your goal is only to display some HTML as a part of your UI, this is probably fine; the user won’t need to interact with the web page beyond reading it, and the web page won’t need to interact with the user. If you actually want a full-blown web browser, then you probably want to invoke the Browser application with a URL Intent rather than show it with a WebView.

Since we are loading a local html file, there is no need to use internet permission, if you want learn how to load a web URL in android WebView, I will suggest you read my post on the topic – How to load external web page inside Android WebView.

What we will do is the put the html file we wish to load in android WebView to our assets folder in our android project.

Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: LoadingHtmlFileInAndroidWebView

Package: com.inducesmile.loadinghtmlfileinandroidwebview

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package name.


Open activity_main.xml file, drag and drop a WebView control to your layout. Set the width and height of the WebView to match_parent. It is so simple. The code snippet is shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_alignParentStart="true" />



In the MainActivity.java file, we will get the instance of the WebView control. With the WebView setting method, we will set the zoom level of the WebView content and also enable Javascript.

The AssetManager object is use to convert the html file in the assets folder to an inputstream object which is subsequently converted to string by StreamToString() method. The final code in this file is shown below.

package inducesmile.com.loadinghtmlfileinandroidwebview;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {

        WebView htmlWebView = (WebView)findViewById(R.id.webView);
        WebSettings webSetting = htmlWebView.getSettings();

        String htmlFilename = "bio10.html";
        AssetManager mgr = getBaseContext().getAssets();
        try {
            InputStream in = mgr.open(htmlFilename, AssetManager.ACCESS_BUFFER);
            String htmlContentInStringFormat = StreamToString(in);
            htmlWebView.loadDataWithBaseURL(null, htmlContentInStringFormat, "text/html", "utf-8", null);

        } catch (IOException e) {
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;

        return super.onOptionsItemSelected(item);
    public static String StreamToString(InputStream in) throws IOException {
        if(in == null) {
            return "";
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
        } finally {
        return writer.toString();

Save the file and run your project. If everything works for you, the project will appear like this in your device.

Loading Html File from Assets folder in Android WebView

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published


  1. Avatar
  2. Avatar
    • Inducesmile
      • Avatar
        • Inducesmile
          • Avatar
  3. Inducesmile

Add a Comment