WebView Based Android App with ProgressBar and SwipeRefreshLayout Android by Rajesh Kumar Sahanee - July 22, 2019July 22, 20190 Post Views: 7,059 Hello Friends, Today we are going to see another example of WebView based App, but this time it will be WebView Based Android App with ProgressBar and SwipeRefreshLayout. Here ProgressBar helps us to see progress of page loading while SwipeRefreshLayout is used to refresh the same page with just a swipe. To Refresh page you just need to swipe down side and your page will be reloaded, but if you want to see my previous example check WebView Based Android App post. In this example there is one more feature that is accidental back protection, which stops closing app from accidental back button pressed, we need to tap twice on back button to close this app. So, here is the code. MainActivity.java MainActivity.java Java package com.zatackcoder.webviewapp2; import android.app.ProgressDialog; import android.content.Context; import android.graphics.Bitmap; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.http.SslError; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.webkit.GeolocationPermissions; import android.webkit.SslErrorHandler; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { SwipeRefreshLayout swipeRefreshLayout; ProgressBar progressBar; WebView webView; String url = "https://zatackcoder.com"; boolean doubleBackPressed = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initControls(); } private void initControls() { swipeRefreshLayout = findViewById(R.id.swiperefreshlayout); progressBar = findViewById(R.id.progressbar); webView = findViewById(R.id.webview); //setting webviewclient webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //To open hyperlink in existing WebView view.loadUrl(url); return false; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show(); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { progressBar.setVisibility(View.VISIBLE); progressBar.setProgress(0); } @Override public void onPageFinished(WebView view, String url) { progressBar.setVisibility(View.GONE); swipeRefreshLayout.setRefreshing(false); } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Log.d("MainActivity", "SSL Error"); super.onReceivedSslError(view, handler, error); } }); //setting webchromeclient webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView webView, int newProgress) { progressBar.setProgress(newProgress); } @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { // Always grant permission since the app itself requires location // permission and the user has therefore already granted it callback.invoke(origin, true, false); } }); //setting other settings webView.getSettings().setJavaScriptEnabled(true); webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); webView.getSettings().setBuiltInZoomControls(false); webView.getSettings().setLoadsImagesAutomatically(true); webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setGeolocationEnabled(true);// For geolocation //checking internet connection if (!isNetworkConnected()) { webView.loadData("<html><body style='text-align:center;'><h1>Connection Error ...</h1><h2>Check Your Connection ... </h2></body></html>", "text/html", null); } else { webView.loadData("<html><body style='text-align:center;'><h1>Loading ...</h1></body></html>", "text/html", null); webView.loadUrl(url); } //setting swiperefreshlistener swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { webView.reload(); } }); } private boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); return ni == null ? false : true; } @Override public void onBackPressed() { if(webView.canGoBack()) { webView.goBack(); } else { if(doubleBackPressed) { super.onBackPressed(); return; } doubleBackPressed = true; Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show(); } //restoring back press after 2 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackPressed=false; } }, 2000); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 package com.zatackcoder.webviewapp2; import android.app.ProgressDialog;import android.content.Context;import android.graphics.Bitmap;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.net.http.SslError;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.webkit.GeolocationPermissions;import android.webkit.SslErrorHandler;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.ProgressBar;import android.widget.Toast; public class MainActivity extends AppCompatActivity { SwipeRefreshLayout swipeRefreshLayout; ProgressBar progressBar; WebView webView; String url = "https://zatackcoder.com"; boolean doubleBackPressed = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initControls(); } private void initControls() { swipeRefreshLayout = findViewById(R.id.swiperefreshlayout); progressBar = findViewById(R.id.progressbar); webView = findViewById(R.id.webview); //setting webviewclient webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //To open hyperlink in existing WebView view.loadUrl(url); return false; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show(); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { progressBar.setVisibility(View.VISIBLE); progressBar.setProgress(0); } @Override public void onPageFinished(WebView view, String url) { progressBar.setVisibility(View.GONE); swipeRefreshLayout.setRefreshing(false); } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { Log.d("MainActivity", "SSL Error"); super.onReceivedSslError(view, handler, error); } }); //setting webchromeclient webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView webView, int newProgress) { progressBar.setProgress(newProgress); } @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { // Always grant permission since the app itself requires location // permission and the user has therefore already granted it callback.invoke(origin, true, false); } }); //setting other settings webView.getSettings().setJavaScriptEnabled(true); webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); webView.getSettings().setBuiltInZoomControls(false); webView.getSettings().setLoadsImagesAutomatically(true); webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setGeolocationEnabled(true);// For geolocation //checking internet connection if (!isNetworkConnected()) { webView.loadData("<html><body style='text-align:center;'><h1>Connection Error ...</h1><h2>Check Your Connection ... </h2></body></html>", "text/html", null); } else { webView.loadData("<html><body style='text-align:center;'><h1>Loading ...</h1></body></html>", "text/html", null); webView.loadUrl(url); } //setting swiperefreshlistener swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { webView.reload(); } }); } private boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); return ni == null ? false : true; } @Override public void onBackPressed() { if(webView.canGoBack()) { webView.goBack(); } else { if(doubleBackPressed) { super.onBackPressed(); return; } doubleBackPressed = true; Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show(); } //restoring back press after 2 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackPressed=false; } }, 2000); }} activity_main.xml activity_main.xml XHTML <?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/progressbar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="4dp" /> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" /> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout> </android.support.constraint.ConstraintLayout> 12345678910111213141516171819202122232425262728293031323334353637 <?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/progressbar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="4dp" /> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiperefreshlayout" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" /> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout> </android.support.constraint.ConstraintLayout> AndroidManifest.xml AndroidManifest.xml XHTML <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.webviewapp2"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <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> 1234567891011121314151617181920212223242526 <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.webviewapp2"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <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> Video https://zatackcoder.com/wp-content/uploads/2019/07/webview-app-2.mp4 AndroidStudio Project Download WebView Based Android App with ProgressBar and SwipeRefreshLayout 1 file(s) 9.13 MB Download Thanks for Stoping by If you find this helpful then please share