Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Loading local html-page into webview?

OP DANNEMAN101

28th March 2009, 04:12 PM   |  #1  
OP Junior Member
Thanks Meter: 0
 
15 posts
Join Date:Joined: Dec 2008
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?
28th March 2009, 04:56 PM   |  #2  
Member
Thanks Meter: 7
 
77 posts
Join Date:Joined: Feb 2009
More
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: [ View ]
28th March 2009, 06:48 PM   |  #3  
OP Junior Member
Thanks Meter: 0
 
15 posts
Join Date:Joined: 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.
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes