You are on page 1of 2

Web View

This is a sample activity which shows How to invoke web browser from your application.

Basic description of algorithm in step by step form: 1. Create a Project WebViewDemo. 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 10 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:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 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://m.google.com/"); }

17. } 2. Add the relevant permissions to your AndroidManifest.xml file:


1. <?xml version="1.0" encoding="utf-8"?> 2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3. package="com.sample.WebViewDemo" 4. android:versionCode="1" 5. android:versionName="1.0"> 6. 7. <uses-sdk android:minSdkVersion="8" /> 8.

9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.

<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:


1. <?xml version="1.0" encoding="utf-8"?> 2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. android:orientation="vertical" 4. android:layout_width="fill_parent" 5. android:layout_height="fill_parent"> 6. <WebView 7. android:id="@+id/web_view" 8. android:layout_width="fill_parent" 9. android:layout_height="fill_parent" 10. android:layout_weight="1.0" 11. /> 12. </LinearLayout>

4. Run the application. Its running? 5. Demo and Run, again WebView allow you to manually load custom HTML markup, via webView.loadData(), see modified version :
14. //webView.loadUrl("http://www.google.com"); 15. String customHtml = 16. "<html> <body>Hello <b>Android</b> Web View</body>html>"; 17. webView.loadData(customHtml, "text/html", "UTF-8");

You might also like