Tuesday, February 26, 2013

Android HTML Viewer


This example shows how you can show HTML files into your application.
1.) Create a new project by File-> New -> Android Project name it suitably.
2.) Put a .html file into your assets folder, name it “example.htm” and write following code into it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
    <title>HTML viewer</title>
    <style type="text/css">
        body
        {
            padding: 0px;
            font-size: 11pt;
            color: white;
            font-family: Arial;
            background-color: #0000FF;
        }
         
        .style1
        {
            color: #FFFFFF;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="Help" id="Help" style="Z-INDEX: 1000; LEFT: 5%; OVERFLOW: visible; WIDTH: 90%; POSITION: absolute; TOP: 20px; HEIGHT: 500px">
        <h3>HTMLViewerExample: </h3>
        <p>
            You can place your information in this HTML document.
        </p>
    </div>
</body>
</html>



The code for the mainactivity class




import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
 
public class HTMLViewerExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView wb = new WebView(this);
        wb.loadUrl("file:///android_asset/example.htm");
        setContentView(wb);
    }
}

the output is as shown below




No comments:

Post a Comment