Feb 23, 2011

Android Webview example(opening web site)

Let us discuss this in several parts. In this first part let load a normal web page to application using webview.Webview is primarily used for loading a html content  in our application.

loadUrl is  the prime method to load a particular webpage to webview.
Hence make use of the snippet to load a page.

public class WebViewExample extends Activity {

 public void onCreate(Bundle savedInstanceState) {

 WebView webView = (WebView) findViewById(R.id.webview);

 webView.setWebViewClient(new HelloWebViewClient());

But the above method loads the browser instead of opening the webpage inside the application.
To overcome this difficulty we should use the method shouldOverrideUrlLoading() along with webview and url string.
Now the application opens the web page inside the application itself.

public class HelloWebViewClient extends WebViewClient {

 public boolean shouldOverrideUrlLoading(WebView view, String url) {
 return true;

Layout design
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:padding="10px">
 <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AndroidPeople.com - Webview " android:textStyle="bold" android:textSize="20sp" android:textColor="#fff"></TextView><WebView android:id="@+id/webview" android:layout_width="fill_parent"
  android:layout_height="fill_parent" />


out put : 

For Project Source Code Click here :

one more important thing here we have to give permission to manifest file to add permission tag ,

permission appers like this :

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

No comments:

Post a Comment