I made a webview app but there is one error which is bothering me. The error is with the "webView.setWebViewClient(new WebViewClient()); and onErrorReceived" I created a no_internet_layout to show when there is no connection and added it to the "onErrorReceived" implementation. Now instead of it to show only when there is no internet connection it also shows on little errors received whiles there is connection.
Here is the code for no_internet_layout display
//no internet screen
public void internetcheck() {
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobiledata = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mobiledata.isConnected()) {
webView.setVisibility(View.VISIBLE);
swipeRefreshLayout.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
webView.reload();
} else if (wifi.isConnected()) {
webView.setVisibility(View.VISIBLE);
swipeRefreshLayout.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.GONE);
webView.reload();
} else {
webView.setVisibility(View.GONE);
swipeRefreshLayout.setVisibility(View.VISIBLE);
relativeLayout.setVisibility(View.VISIBLE);
}
}
The code for "webView.setWebViewClient(new WebViewClient()); and onErrorReceived"
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
internetcheck();
super.onReceivedError(view, request, error);
}
});
I tried showing a text when there is an error.....
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Toast.makeText(getApplicationContext(), "Poor Internet Connection", Toast.LENGTH_LONG).show();
super.onReceivedError(view, request, error);
}
});
...but when the user looses internet connection in the middle of using the app,it shows the default no_connection_page (which is not accepted) instead of the customized no_internet_page.
Please how can i show the no_internet_page in the middle of using the app only when internet connection is lost... ..and also show toast test when there are little errors but there is internet connection. Please someone help me..