Thursday 19 September 2013

WebView in Android

Using a web view to load a page from Internet


WebView is nothing but the simple view created within the application so as to load the web pages within the application and not exit the application and load in the inbuilt browser.

1.MainActivity.java

package com.sri.webview;

import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;

public class MainActivity extends Activity {
WebView wb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wb=(WebView) findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
wb.loadUrl("http://www.google.co.in");
}
}


2.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

3.Add permission in the android manifest file

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

4.That's it

No comments:

Post a Comment