Display Static HTML page in Android (Eclipse) emulator

December 28th, 2010 by Nikhil

For a few hours now I have been looking at solutions to run/test my static HTMLs in the Eclipse/Android emulator.  Have not been totally successful yet…. what I mean by “Totally” is the HTML gets loaded OK but all the CSS and JS, i.e. the visual expression is  not seen. In developer words, the CSS is not applied to the page…

Thought it would a few   fellow newbie Android  UI developers , If  I noted the snippet down that I used to  display the  HTML .

AND Android gurus, PLEASE add your comments to help us novices in mobile application UI developers further!

Sorry! but Im assuming that you  know how to create a  Android Mobile Application in Eclipse.

Create this as your activity; This will read the file ‘index.html’ from your project assets/ folder.

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webview = new WebView(this);
        setContentView(webview);
        try {
            InputStream fin = getAssets().open("index.html");
                byte[] buffer = new byte[fin.available()];
                fin.read(buffer);
                fin.close();
                webview.loadData(new String(buffer), "text/html", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

get ExpressingIT News by Email Subscribe to ExpressingIT by Email |  Follow Me on Twitter


Leave a Reply

 Subscribe to ExpressingIT RSS
get ExpressingIT News by Email Subscribe to ExpressingIT by Email
 Follow Me on Twitter