Webview In Android


A Webview enables you to deliver a web application (or just a web page) as a part of a client application. It allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.

A common scenario in which using WebView is helpful is when you want to provide information in your application that you might need to update, such as an end-user agreement or a user guide. Within your Android application, you can create an Activity that contains a WebView, then use that to display your document that's hosted online.


Using the webview is a  very simple task:
1. Declare a UI element for a webview.
xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

android.support.constraint.ConstraintLayout
>


2. Declare an Objectof Webview class:  WebView webView; 

3. Add permission for Internet in the Manifest file :

AndroidManifest.xml

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

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

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

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

manifest>


4. Instantiate webview variable :webView=(WebView)findViewById(R.id.webview);

5. Copy the following commands to load a specific URL :

MainActivity.java:

package com.ourcoaching.simplewebviewapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

private WebView webView;
private final String Website_URL="https://www.ourcoaching.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView=(WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(Website_URL);
}
}


Thus your webview application will work perfectly.


Javascripts and back press are disabled in webview apps. You need to separately add those features.

1. Enable java script content 

webSettings.setJavaScriptEnabled(true);
2. Add back press webview navigation:
myWebView.canGoBack()

Download the code from GitHub



Related Tutorials