Sunday, 3 March 2013

WebView - Load static webpages Demo



Demo:



1)main.xml: 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnShowWebpage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="Show Webpage"
        android:textColor="#FF0000"
        android:textSize="20dp" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

2)Activity:


package com.venky.staticwebpages;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class StaticWebPagesActivity extends Activity {
      /** Called when the activity is first created. */

      WebView webView;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            webView = (WebView) findViewById(R.id.webView);

            Button btnShowWebpage = (Button) findViewById(R.id.btnShowWebpage);

            btnShowWebpage.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        webView.loadUrl("file:///android_asset/webpage.html");
                  }
            });
      }
}

3)webpage.html: (Created in assets folder)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
      <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=windows-1252">
      <TITLE></TITLE>
      <META NAME="GENERATOR" CONTENT="OpenOffice.org 3.4.1  (Win32)">
      <META NAME="CREATED" CONTENT="20130302;23284643">
      <META NAME="CHANGED" CONTENT="20130302;23400248">
      <STYLE TYPE="text/css">
      <!--
            @page { margin: 0.79in }
            P { margin-bottom: 0.08in }
            A:link { so-language: zxx }
      -->
      </STYLE>
</HEAD>
<BODY LANG="en-US" DIR="LTR">
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><FONT COLOR="#ff0000"><FONT SIZE=4><B>Welcome
to Html page</B></FONT></FONT></P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><BR>
</P>
<P ALIGN=LEFT STYLE="text-indent: 0.09in; margin-bottom: 0in"><FONT COLOR="#0000ff"><FONT SIZE=3 STYLE="font-size: 13pt"><B>This
is webpage. Saved in assets folder.</B></FONT></FONT></P>
<P ALIGN=LEFT STYLE="text-indent: 0.09in; margin-bottom: 0in"><FONT COLOR="#0000ff"><FONT SIZE=3 STYLE="font-size: 13pt"><B>By
using load method easily call webpages.</B></FONT></FONT></P>
</BODY>
</HTML>

4) Download this Project Click Here

1 comment:

  1. I'm so googled thousands time finally found it here .....so tanks

    ReplyDelete