Simple Application Using Android Web View

This section shows how to embed a WebView, which could allow you to add HTML UI to your native Android application. Create a project, as you did in the previous tutorial (Figure 3-15).

Figure 3-15. Create Project.

In this example, we don't use a layout (although you could). Instead, we simply create a new WebView and then set the ContentView to that instance of the WebView. Then we dynamically create some html and load it into the WebView (Figure 3-16). This is a very simply example of a powerful concept (Figure 3-17).

package sample.webview;

import android.app.Activity; import android.os.Bundle; import android.webkit.WebView;

package sample.webview;

import android.app.Activity; import android.os.Bundle; import android.webkit.WebView;

public class SampleWebView extends Activity {

/** Called when the activity is first created. */ ^Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); WebView webview - new WebView(this); setContentView(webview);

String hello - "<html><body><p>This could be HTML UI</p></body></html>";| webview.loadData(hello, "text/html", "utf-8");

Figure 3-16. Code for adding a WebView to SampleWebView.java

Figure 3-16. Code for adding a WebView to SampleWebView.java

Figure 3-17. Application with WebView running in the Android simulator

For more details on different ways to use WebView in Android, see http://developer.android.com/reference/android/webkit/WebView.html.

Living With Android

Living With Android

Get All The Support And Guidance You Need To Finally Be A Success At Using Android. This Book Is One Of The Most Valuable Resources In The World When It Comes To Playing With the Hottest Android Tablet.

Get My Free Ebook


Post a comment