Tuesday, March 22, 2011

Webview in Android

Webview in Android

DOWNLOAD SOURCE CODE


Webview is primarily used for loading a html content  in our application.

Let us discuss this in several parts. In this first part let load a normal web page to application using webview.

loadUrl is  the prime method to load a particular webpage to webview.

Hence make use of the snippet to load a page.


screen shot 


sourcecode


WebviewActivity.class

package com.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebviewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://iamvijayakumar.blogspot.com/");

        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.

 HelloWebViewClient.class

package com.webview;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

}






main.xml 
 


<?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="iamvijayakumar.blogspot.com  " 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" />


</LinearLayout>

No comments:

Post a Comment

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...