In this tutorial, we will develop an android application using Webview to load a web page. As a part of this tutorial, I will show you how you can detect telephone links “tel:”, email links “mailto”, load PDF files and also check for internet connection. I’ve also used a progress bar to determine whether web page is fully loaded.
Language: Java
Software: Android Studio 3.1
Software: Android Studio 3.1
Testing
Classes: MainActivity.java, WebViewAndroid.java, activity_main.xml, activity_webview, strings.xml, AndroidManifest.xml
CODE:
MainActivity.java
package com.subzdesigns.webviewandroid;
/**
* Created by Subz Designs.
* Developed by Tansu Canturk
*/
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public MainActivity activity;
boolean isConnected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.activity = this;
Button button1 = (Button) findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isConnectingToInternet(getApplicationContext())) {
Intent intent = new Intent(MainActivity.this, WebViewAndroid.class);
intent.putExtra("Id", 1);
startActivity(intent);
} else {
CheckCon();
}
}
});
}
public boolean isConnectingToInternet(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
public void CheckCon() {
Toast.makeText(getApplicationContext(), "Check your network connection and try again.", Toast.LENGTH_SHORT).show();
}
}
WebViewAndroid.Java
package com.subzdesigns.webviewandroid;
/**
* Created by Subz Designs.
* Developed by Tansu Canturk
*/
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class WebViewAndroid extends AppCompatActivity
{
Boolean isLoaded = false;
int id;
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
TextView titletextview = (TextView)findViewById(R.id.title);
Button backbtn =(Button)findViewById(R.id.backBtn);
Intent mIntent = getIntent();
id = mIntent.getIntExtra("Id", 0);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String website = getString(R.string.website);
if(id==1)
{
setTitle("Webview");
startWebView(website);
}
backbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertAction();
}
});
}
private void startWebView(String url) {
webview.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
else if (url.contains("mailto:")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}else if (url.contains(".pdf")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse("https://docs.google.com/gview?embedded=true&url=" + url)));
return true;
}else {
view.loadUrl(url);
return true;
}
}
public void onLoadResource (WebView view, String url) {
if (isLoaded == false) {
isLoaded = true;
progressDialog = new ProgressDialog(WebViewAndroid.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try{
progressDialog.dismiss();
}catch(Exception exception){
exception.printStackTrace();
}
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(url);
}
public void AlertAction () {
AlertDialog alertDialog = new AlertDialog.Builder(WebViewAndroid.this).create();
alertDialog.setMessage("Go back to main activity?");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alertDialog.setButton(AlertDialog.BUTTON1, "CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webview.canGoBack()) {
webview.goBack();
} else {
AlertAction();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
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="com.subzdesigns.webviewandroid.MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="#d0272c"
android:text="Click to load"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>
activity_web.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.subzdesigns.webviewandroid.WebViewAndroid">
<LinearLayout
android:id="@+id/customToolbar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#d0272c"
android:orientation="horizontal">
<Button
android:id="@+id/backBtn"
style="?android:attr/borderlessButtonStyle"
android:layout_width="75dp"
android:layout_height="35dp"
android:background="#d0272c"
android:text="Close"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/customToolbar"
/>
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">Webview Android</string>
<string name="website">https://subzdesigns.com/blog/android-webview-tel-mailto-pdf-check-internet-connection</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.subzdesigns.webviewandroid">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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>
<activity android:name=".WebViewAndroid">
</activity>
</application>
</manifest>
Screenshots:




Source code: