Wednesday, February 20, 2013

How to display webpage from website on Android using WebView class

Android WebView class allows viewing a specified website URL from within an Android app without opening a web browser.

1. Create a new JAVA class called and paste the following code

package com.samples.webexample;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewer extends DashboardActivity

 private WebView webView;
protected void onCreate(Bundle savedInstanceState)

  // Create reference to UI elements       
  webView  = (WebView) findViewById(;       
  // workaround so that the default browser doesn't take over       
  webView.setWebViewClient(new MyWebViewClient());          

  // Setup click listener  

 /** Opens the URL in a browser */   
 private void openURL() {    


 private class MyWebViewClient extends WebViewClient {       
  public boolean shouldOverrideUrlLoading(WebView view, String url) {           
   return true;       

2. Create a new layout file named main.xml and paste the following code

<LinearLayout xmlns:android=""

                      android:layout_weight="1.0"    />

3. Add the following line to your Android manifest file if you are calling this activity from another activity

      <activity android:name=".WebViewer">

No comments:

Post a Comment