Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,732,914 Members 50,498 Now Online
XDA Developers Android and Mobile Development Forum

Loading local html-page into webview?

Tip us?
 
DANNEMAN101
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 15
Join Date: Dec 2008
Default Loading local html-page into webview?

I have a large set of html-pages (css-formatted, + some javascript) that I want saved inside the project so the user dont have to go online to browse for them from the app.


1) My first question is how do I open a a local html-page (named, for instance "myWebPage.html") in a WebView?

I know I can make a WebView load a html-string with the following code:

main.xml
Code:
<?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"
    >
<WebView android:id="@+id/webkit"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
/>
</LinearLayout>
WebKitTest.java
Code:
package com.androidspanishcourse.WebKitTest;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebKitTest extends Activity {
	
    // Declare webview
    WebView browser;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // ?
        browser=(WebView)findViewById(R.id.webkit);
        
        // Loads html-string into webview
        browser.loadData("<html><body>Hi dude<br><br>Hi again</body></html>", "text/html", "UTF-8");
    }
}
But I dont know how to load a specific local html-page.


2) My second question is: where do I put the html-pages? I understand that resources are generally put in the /res/-folder of the project, but do I put them in a specific sub-folder of the res-folder?
 
waacow
Old
#2  
Member
Thanks Meter 6
Posts: 77
Join Date: Feb 2009
http://developer.android.com/referen...t/WebView.html

1.Webview.loadUrl(String url)

2.The prefix "file:///android_asset/" will cause WebView to load content from the current application's 'assets' folder. For example, a myimage.gif file in the /assets folder can be used in the html image tag as:
<img src="file:///android_asset/myimage.gif">. Same applies to html file.
The Following User Says Thank You to waacow For This Useful Post: [ Click to Expand ]
 
DANNEMAN101
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 15
Join Date: Dec 2008
Thanks a lot, waacow

Also, apparently you need to add "browser.getSettings().setJavaScriptEnabled(true); " for the webview to be able to handle javascript.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes