Android WebView Example with easy explanation

Android webview is used to display the webpage inside your application

Webview is used to turn your android  application into the web application.

Webview uses the webkit to display the webpage.

There are some steps to use WebView in your application.

1.To use the webview in your application,you have to add <Webview>  element in xml layout.

The webview layout format is given below:

<Webview
android id=”@+id/webview”
android layout_height=”match_parent”
android layout_width=”match_parent”/>

2.After that you have to get the reference of webview into java class by using findViewById.

Webview webviewObject=(Webview)findViewById(R.id.webview);

3.To load the url into the webview,we have to call loadUrl(String url) of the webview class.

The syntax as follow

webviewObject.loadUrl(“https://www.placementpreparation.com”);

Example of WebView

1.Android Layout files

File:res/layout/webview.xml

webview.xml

<?xml version="1.0" encoding="utf-8" ?>
<RlativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="mathch_parent"
android:layout_height="match_parent"
android:Orientation="vertical"/>

<Webview
android id=”@+id/web”
android layout_height=”match_parent”
android layout_width=”match_parent”/>

</RelativeLayout>

2.Activity

MainActivity.java

import android.activity.Activity;
import android.os.Bundle;
import android.webkit.Webview;
import android.webkit.WebViewClient;

Public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

//Inflating the webview.xml layout into the MainActivity class
setContentView(R.layout.web);

//Finding the webview Id
Webview webview=(Webview)findViewById(R.id.web);

//Loading the https://www.placementpreparation.com into the webview
webview.loadUrl(“https://www.placementpreparation.com”);


//Enabling the javascript
webview.getSettings.setJavaScriptEnabled(true);

//WebViewClient allow to handle onPageFinished and Override Url loading
webview.setWebviewClient(new myBrowser( ));
}

Private class myBrowser extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(Webview v,String url)
{
v.loadurl(url);
return true;
}
}
}

3.Android Manifest

Webview required INTERNET permission to load url

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

4.Output

Example of WebView

Hope,I explained step by step about the android webview.If still have any query then you can comment me.

Thanks and Regards

Placement preparation 

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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