Android tutorial – display embedded HTML file in WebView
- February 8th, 2011
- Posted in Tutorials
- Write comment
Working on Power Schedule, I wanted to make a short help file, with some formatting. It seems there were several ways of doing this, but I chose to include an embedded HTML file into my application. It seemed easier to just edit an HTML file in an editor.
So let’s get started…
- first, embed an html file in the app resources. For this, create the “raw” subfolder in “res” folder and add your HTML formatted file:
- use a WebView view in your layout file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webviewHelp android:layout_width="fill_parent" android:layout_height="fill_parent"/>
- use this code in your activity onCreate() method:
WebView webview = (WebView) findViewById(R.id.webviewHelp); webview.loadData(readTextFromResource(R.raw.help), "text/html", "utf-8");
where readTextFromResource is something like this:
private String readTextFromResource(int resourceID) { InputStream raw = getResources().openRawResource(resourceID); ByteArrayOutputStream stream = new ByteArrayOutputStream(); int i; try { i = raw.read(); while (i != -1) { stream.write(i); i = raw.read(); } raw.close(); } catch (IOException e) { e.printStackTrace(); } return stream.toString(); }
It will look something like this:

I hope it helps


I believe the code (R.raw.webview) in the onCreat method should be (R.raw.webviewHelp) to refer to the layout file id.
That’s right – I modified the source while editing the article. Thanks for the correction
Thanks man, very usefull
Hi,
I have created android application to get the contact list from android phone. In this i’m using Webview.loadurl() to load the home page(index.htm) like,
webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl(“file:///android_asset/assets/www/index.htm”);
This is working in android emulator and phone, not working in “AcerICONIA TAB A50″
showing an error ” webpage is not available at file:///android_asset/assets/www/index.htm ”
Please help me to clear this.
That’s another way to load the file – personally I didn’t try it, so I don’t know why it doesn’t work on Acer. Perhaps you can try loading it the way I did?
Hi,
Thanks for your reply.
I got a solution.
webview.loadUrl(“file:///android_asset/assets/www/index.htm”);
in this, no need to include “assets” folder in the url.
Answer: webview.loadUrl(“file:///android_asset/www/index.htm”);
Thank you.
Thanks a lot for sharing this
Thanks this is what i was looking for when I wanted to have a page be coded in html instead of using text views in xml.
myWebView.loadUrl(“file:///assets/index.html”); is not working for me on loading the webview the emulator says “Web page not found at file:///assets/index.html”
i have placed the html file directally under assets folder in android project
myWebView.loadUrl(“file:///android_asset/index.html”);
this ll work
thanks very useful but one problem stat view link is no accept why pls code provide
Hello,
Thank you very much for the code, I have added an .gif figure the the html using , this is displayed well in the layout in java or any browser, however if I run the emulator this figure will not show.
Well, I didn’t include any pictures – how did you reference the image URL in the HTML file?
Hello
Thanks for nice tutorial.
Now i need to stop vertical scroll and give horizontal scroll to webiview
Thanks
Parag
Guys, by putting your HTML in the assets instead of raw you’ll be free of the stream code, you’ll be able to include images, and you’ll be able to have links. Go for assets.
I cant get mine to load at all: Html is in assets folder:
CODE:
Dialog dialog = new Dialog(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.option_dialog, null);
dialog.setContentView(vi);
dialog.setTitle(“Standards”);
//dialog.setCancelable(true);
WebView wb = (WebView) vi.findViewById(R.id.webview);
wb.loadUrl(“file:///android_asset/en_prep_opt_1.html”);
System.out.println(“..loading url..”);
dialog.show();
END CODE
I get the title, but not any content from the asset html.
my layout just looks like this: