Monday, 24 March 2008

Rendering Web Pages inside Android Activity

While you create Android activities, you supply the activity view content either through XML or dynamically through the code. But, sometimes the content in the view, specially the static content (content will not change with different runs) may be a web page; e.g. “Help” Activity or “Terms and Conditions” Activity of your application.

These contents can be done as web pages, and that will help you in changing the content without affecting the client side application binary. However, sometimes you need to open those pages inside your application as activity, not through web-browser, e.g. open “Terms and Conditions” web page for the user to accept or reject through button actions.

Android have a great widget for this usage, it is “WebView” component. It accepts either plain text as you use in Text View component or a URL to a web page to render inside your activity.

The following example show a layout for a sample activity contains web view to show eSpace site home page:
  • You first create the layout XML file contains your component as following:

    <?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/myWebView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

    </LinearLayout>
  • And then supply the URL to the web view through your activity “onCreate” method code:

    WebView webView = (WebView) findViewById(R.id.myWebView);
    webView.loadUrl(“http://www.espace.com.eg”);

  • Now, the given URL is rendered inside the web view widget and you can deal with the opened web page with all functionality available from Android web-kit (scroll, zoom in, zoom out, etc).

1 comment:

Anonymous said...

This advice is actual interesting, I absolutely enjoyed, I would like get added advice about this, because is actual beautiful, acknowledgment for sharing

Android app developer