Professional Documents
Culture Documents
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/"); }
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>
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");