Android Example of Webview

      No Comments on Android Example of Webview

Android WebView is used to load web page in android. The web page can be loaded in 2 ways:-
1. from same application
2. from URL.
It is used to display online content in android activity.

For this you have to give permission in AndroidManifest.xml

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

activity_main.xml

Drag the Webview from the pallete.Now the layout file will look like 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: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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignTop="@+id/textView1" />

</RelativeLayout>

MainActivity.java

Write the code in the main activity file

package com.example.scanfcode;

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


public class MainActivity extends Activity
{
	WebView w1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		w1=(WebView)findViewById(R.id.webView1);
		
		w1.loadUrl("http://google.com/");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
OUTPUT:-

scanfcode

Leave a Reply

Your email address will not be published. Required fields are marked *