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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?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
AndroidStudio Project Download
WebView Based Android App with ProgressBar and SwipeRefreshLayout
1 file(s) 9.13 MB
Thanks for Stoping by
If you find this helpful then please share
Comments