Académique Documents
Professionnel Documents
Culture Documents
Chapter
13
As with any other widget, you need to tell it how it should fill up the space in the layout. In this case, it fills all remaining space.
141
142
The only unusual bit with this edition of onCreate() is that we invoke loadUrl() on the WebView widget, to tell it to load a web page (in this case, the home page of some random firm). However, we also need to make one change to AndroidManifest.xml, requesting permission to access the Internet:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.commonsware.android.browser1"> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/cw"> <activity android:name=".BrowserDemo1" android:label="BrowserDemo1"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
If we fail to add this permission, the browser will refuse to load pages. Permissions are covered in greater detail in Chapter 28. The resulting activity looks like a web browser, but with hidden scrollbars, as shown in Figure 131.
143
As with the regular Android browser, you can pan around the page by dragging it. The D-pad moves you around all the focusable elements on the page. What is missing is all the extra stuff that makes up a web browser, such as a navigational toolbar. Now, you may be tempted to replace the URL in that source code with something that relies on JavaScript, such as Googles home page. By default, JavaScript is turned off in WebView widgets. If you want to enable JavaScript, call getSettings().setJavaScriptEnabled(true); on the WebView instance.
Loading It Up
There are two main ways to get content into the WebView. One is to provide the browser with a URL and have the browser display that page via loadUrl(), as described in the previous section. The browser will access the Internet through whatever means are available to that specific device at the present time (Wi-Fi, cellular network, Bluetoothtethered phone, well-trained tiny carrier pigeons, etc.). The alternative is to use loadData(). Here, you supply the HTML for the browser to view. You might use this to do the following: Display a manual that was installed as a file with your application package. Display snippets of HTML you retrieved as part of other processing, such as the description of an entry in an Atom feed.
144
Generate a whole UI using HTML, instead of using the Android widget set. There are two flavors of loadData(). The simpler one allows you to provide the content, the MIME type, and the encoding, all as strings. Typically, your MIME type will be text/html and your encoding will be UTF-8 for ordinary HTML. For example, you could replace the loadUrl() invocation in the previous example with the following:
browser.loadData("<html><body>Hello, world!</body></html>", "text/html", "UTF-8");
145
canGoBack(): Determines if there is any history to go back to. goForward(): Goes forward one step in the browser history. canGoForward(): Determines if there is any history to go forward to. goBackOrForward(): Goes backward or forward in the browser history. A negative number as an argument represents a count of steps to go backward. A positive number represents how many steps to go forward. canGoBackOrForward(): Determines if the browser can go backward or forward the stated number of steps (following the same positive/negative convention as goBackOrForward()). clearCache(): Clears the browser resource cache. clearHistory(): Clears the browsing history.
146
setContentView(R.layout.main); browser=(WebView)findViewById(R.id.webkit); browser.setWebViewClient(new Callback()); loadTime(); } void loadTime() { String page="<html><body><a href=\"clock\">" +new Date().toString() +"</a></body></html>"; browser.loadDataWithBaseURL("x-data://base", page, "text/html", "UTF-8", null); } private class Callback extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { loadTime(); return(true); } } }
Here, we load a simple web page into the browser (loadTime()) that consists of the current time, made into a hyperlink to the /clock URL. We also attach an instance of a WebViewClient subclass, providing our implementation of shouldOverrideUrlLoading(). In this case, no matter what the URL, we want to just reload the WebView via loadTime(). Running this activity gives the result shown in Figure 133.
147
Selecting the link and clicking the D-pad center button will select the link, causing the page to be rebuilt with the new time.
148