simplyeasylearning

Thursday 20 November 2014

How to Create a Web View in Android Application

WEB VIEW :-
A WebView is used to dispaly web pages or to display some online content in your activity. It can either display remote web pages or load static HTML data. For this you need to give permission to the Android menifest.xml file. For that we need to first use the getsettings() method to get the object that handles settings of the WebView. You need to set the setJavaScriptEnables() to true to enable the JavaScript because JavaScript is disabled for the WebView.



Step 1

Create an XML file and write this:
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
        android:background="#81a3d0">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
            android:text="www.google.com"/>

</RelativeLayout>Step 2Create another XML file with the following:
 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <WebView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/webView"
            android:layout_gravity="center">

            </WebView>

</LinearLayout>Step 3Create a Java class file and write this:
package com.webviewexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(MainActivity.this,Web.class);
         startActivity(i);
            }
        });


    }
}Step 4Create another Java class and write this:
 
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
        android:background="#81a3d0">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
            android:text="www.google.com"/>

</RelativeLayout>Step 4Change the Android menifest,xml file so that it is like this:

 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.webviewexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.webviewexample.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
                android:name=".Web">

        </activity>
    </application>
</manifest>
Step 5
Clipboard04.jpgStep 6

Clipboard02.jpg

0 comments:

Post a Comment

Popular Posts

Total Visitors

TutorialsHub. Powered by Blogger.

Contributors

Tutorials

Search This Blog