Professional Documents
Culture Documents
Open it up and declare a uses-permission element where you specify android.permission.INTERNET as the android:name attribute, like so (line 6):
view plaincopy to clipboardprint? 1. <?xml version="1.0" encoding="utf-8"?> 2. 3. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 4. package="com.webkitdemo"> 5. 6. <uses-permission android:name="android.permission.INTERNET" /> 7. 8. <application android:icon="@drawable/icon" android:label="WebKitDemo"> 9. 10. <activity android:name=".WebKitDemo"> 11. <intent-filter> 12. <action android:name="android.intent.action.MAIN"/> 13. <category android:name="android.intent.category.LAUNCHER"/> 14. 15. </intent-filter> 16. </activity> 17. </application> 18. </manifest>
If you start your application in the Android emulator now, you will see a blank, white screen. Why? Well, we havent told WebView to render anything yet thats what well do next.
This snippet of code accesses the WebView widget via its resource ID and invokes loadUrl(). Start the app in the emulator and view the result: it should display the site youre currently on!
Comment out the engine.loadUrl() line and insert the following code underneath it:
view plaincopy to clipboardprint? 1. String data = "<html>" + 2. "<body><h1>Yay, Mobiletuts+!</h1></body>" + 3. "</html>"; 4. engine.loadData(data, "text/html", "UTF-8");
Compile and restart the application in the emulator and you should see this:
Technically, you can pass another type of data with its respective mime-type to this method, but most of the time youll be using text/html with a UTF-8 encoding. Dont forget that youre using WebKit, a powerful web engine; you can use CSS3, JavaScript etc. when you design your manuals or anything you want it to display. NOTE: One of WebViews peculiarities is the fact that JavaScript is disabled by default. The reason for this is that in most cases when youre embedding a WebView into your application, youre using it for a help manual or something that doesnt require JavaScript. Nevertheless, its easy to enable it if you do need it:
view plaincopy to clipboardprint? 1. engine.getSettings().setJavaScriptEnabled(true);
Extra features
You may have noticed that the widget doesnt have a toolbar. This is intended, as you would normally launch the native web browser app if you want navigation capabilities. There is, however, a way to control it programmatically. Try invoking one of the following methods on the instance of your widget:
reload(): Refreshes and re-renders the page. goForward(): Goes one step forward in browser history. goBack(): Goes one step back in browser history.
The API has a lot more to offer, but that goes beyond the scope of this Quick Tip.
Conclusion
You should now be familiar with the basic usage of the very useful Android WebView widget. You can use it to display an about web page for your application or a user manual all sorts of stuff. Technically, you can even write your application using JavaScript, CSS and HTML and display it using the widget, although that wouldnt be too practical Or would it?
(Its important to note that this should be attempted on a real device.) I ran into some trouble with sending HTML in an email because it was being interpreted as plain text both in the users email client and in the recipients. Simply changing the MIME type didnt help, but I eventually came across the solution:
view source print?
1 final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 2 emailIntent.setType("text/html"); 3 emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 4 emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body)); 5 startActivity(Intent.createChooser(emailIntent, "Email:"));
Note that both the MIME type is changed and the EXTRA_TEXT is now set as Html.fromHtml(body) instead of just being passed a string with HTML in it. So far Ive only tested this on a Nexus One running Android 2.1, but Ill give it a try on some other platforms and see if it works.