I'm trying to refresh a WebView after regaining internet connection, I have a try again button. If we click on it, the URL will be loaded again. But with the code I've written, this feature is not working.
This is my code:
MainActivity.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
private SwipeRefreshLayout swipeRefreshLayout;
private LinearLayout NoInternetLayout;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progress_horizontal);
swipeRefreshLayout = findViewById(R.id.SwipeRefresh);
NoInternetLayout=findViewById(R.id.NoInternetLayout);
webView = findViewById(R.id.Webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebViewClient());
chekInternet();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
}
@Override
public void onBackPressed() {
if(webView.canGoBack()){
webView.goBack();
}
}
private void chekInternet(){
ConnectivityManager connectivityManager= (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected()){
webView.loadUrl("https://www.google.com/");
webView.setVisibility(View.VISIBLE);
NoInternetLayout.setVisibility(View.INVISIBLE);
}
else if (mobile.isConnected()){
webView.loadUrl("https://www.google.com/");
webView.setVisibility(View.VISIBLE);
NoInternetLayout.setVisibility(View.INVISIBLE);
}
else {
webView.setVisibility(View.INVISIBLE);
NoInternetLayout.setVisibility(View.VISIBLE);
}
}
private class myWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
super.onPageFinished(view, url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
chekInternet();
Button newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent;
intent = new Intent();
startActivity(intent);
chekInternet();
}
});
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/SwipeRefresh"
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"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/progress_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<LinearLayout
android:id="@+id/NoInternetLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="invisible">
<Button
android:id="@+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/refresh"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:src="@drawable/nointernet" />
</LinearLayout>
</RelativeLayout>