How to:‎ > ‎

Display a Web Page

To display a web page in your app, you first need to add INTERNET permission to your manifest.
<uses-permission android:name="android.permission.INTERNET" />
Then, use the WebView class to display a web page.  First, create a layout that contains a webview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <WebView android:id="@+id/myWebView"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

In your Activity, (probably the onCreate), initialize a WebView object by using the layout you created.  An example is below.
    private WebView webview;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.somelayout);

        String url = "http://someurl.com";

        webview = (WebView) findViewById(R.id.myWebView);
        //next line explained below
        webview.setWebViewClient(new MyWebViewClient(this));
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);
    }

If you'd like specific options, like catching pages as they load, you need an inner WebViewClient class.  For example, you can use the onPageStarted(...) method to do something whenever a new page is loaded in your webview:
   public class MyWebViewClient extends WebViewClient {

      public FacebookWebViewClient() {
         super();
         //start anything you need to
      }

      public void onPageStarted(WebView view, String url, Bitmap favicon) {
         //Do something to the urls, views, etc.
      }
   }

More information:
http://developer.android.com/reference/android/webkit/WebView.html

Client:
http://developer.android.com/reference/android/webkit/WebViewClient.html

Other Options:
http://developer.android.com/reference/android/webkit/WebChromeClient.html

Misc. Settings:
http://developer.android.com/reference/android/webkit/WebSettings.html
Comments