Webview android


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 toapplication using webview.
loadUrl is  the prime method to load a particular webpage to webview.
Hence make use of the snippet to load a page.
WebViewExample.java
[sourcecode language="java"]
public class WebViewExample 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://www.androidpeople.com");
webView.setWebViewClient(new HelloWebViewClient());
}
}
[/sourcecode]
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.
[sourcecode language="java"]
public class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
[/sourcecode]
}
Layout design
[sourcecode language="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="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" />
</LinearLayout>
[/sourcecode]
Snapshot
webview-androidpeople example

1 comments :

Nolan Elliott said...

Great reading your blog poost