Monday, June 18, 2012

Web View In Android



This is a sample activity which shows How to invoke web browser from your application.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project WebViewDemo.
2.) Add the relevant permissions to your AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.sample.WebViewDemo"
     android:versionCode="1"

     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".WebViewDemo" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
3.) Put the following code snippet in main.xml:
<?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/web_view"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1.0"   
   />
</LinearLayout>
4.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project. Give the respective project name i.e. WebViewDemo. Enter following information:
Project name: WebViewDemo
Build Target: Google APIs
Application name: WebViewDemo
Package name: com.sample.WebViewDemo
Create Activity: WebViewDemo
On Clicking Finish WebViewDemo code structure is generated with the necessary Android Packages being imported along with WebViewDemo.java. WebViewDemo class will look like following:
package com.sample.WebViewDemo;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewDemo extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                WebView webView = (WebView) findViewById(R.id.web_view);
                webView.loadUrl("http://edumobile.org/android/");
        }
}
Output –The final output:

No comments:

Post a Comment