Prevent Webview from Displaying "Web Page Not Available"

Prevent WebView from displaying web page not available

First create your own error page in HTML and put it in your assets folder, Let's call it myerrorpage.html
Then with onReceivedError:

mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/myerrorpage.html");

}
});

How can I show a custom webpage not available page in Android WebView?

It all came down to simply showing an AlertDialog from onReceivedError:

 @Override
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
//Clearing the WebView
try {
webView.stopLoading();
} catch (Exception e) {
}
try {
webView.clearView();
} catch (Exception e) {
}
if (webView.canGoBack()) {
webView.goBack();
}
webView.loadUrl("about:blank");

//Showing and creating an alet dialog
AlertDialog alertDialog = new AlertDialog.Builder(youractivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("No internet connection was found!");
alertDialog.setButton("Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});

alertDialog.show();

//Don't forget to call supper!
super.onReceivedError(webView, errorCode, description, failingUrl);
}

If you're new to WebView, you'll be looking to implement onReceivedError like this:

mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Code here
}
});

Prevent WebView from loading specific URLs

Implement the method shouldOverrideUrlLoading() as following:

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(request.getUrl().toString().contains("site.com/product/")) {
return true;
}
else {
return super.shouldOverrideUrlLoading(view, request);
}
}

Show toast error and prevent display of webview when no connection is available

if you want to display black screen instead of webview, first you need to change your layout file.
then you check internet connection before load the url.
check if internet connection not available taht time hide your webview and display your Toast message and relative layout.
Othervise hide Relativelayout noConnection.

Step 1 : create layout like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/webViewData"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<RelativeLayout
android:id="@+id/noConnection"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent"/>
</RelativeLayout>

</LinearLayout>

Step 2 : Check your internet connection using this function.

public static boolean checkInternetConnection(Context context) {
if (context != null) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected() && cm.getActiveNetworkInfo().isConnectedOrConnecting();
} else {
return false;
}
} else {
return false;
}

}

At the end do this in your code, before load you url,

public class EarnFragment extends Fragment {
WebView mWebView;
RelativeLayout noConnection;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v=inflater.inflate(R.layout.fragment_earn, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
noConnection = (RelativeLayout)v.findViewById(R.id.noConnection);

// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

if(checkInternetConnection(getActivity())){
noConnection.setVisibility(View.GONE);
mWebView.setVisibility(View.VISIBLE);
mWebView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");

}else{
noConnection.setVisibility(View.VISIBLE);
mWebView.setVisibility(View.GONE);
}

// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());

return v;
}

How to prevent url to show in web view

I have a custom HTML page that will show if something wrong happened when loading the url.

webView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
switch(errorCode){
case ERROR_HOST_LOOKUP:
webView.loadDataWithBaseURL(null,"<YOUR OWN CUSTOM HTML PAGE TO SHOW WHEN THERE'S AN ERROR>", "text/html", "UTF-8",null);
break;
case ERROR_CONNECT:
webView.loadDataWithBaseURL(null,"<YOUR OWN CUSTOM HTML PAGE TO SHOW WHEN THERE'S AN ERROR>", "text/html", "UTF-8",null);
break;
case ...[IF YOU WANT TO CATCH MORE ERRORS]
}
}
}

Reference: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedError(android.webkit.WebView,%20int,%20java.lang.String,%20java.lang.String)

Error Code Reference:
https://developer.android.com/reference/android/webkit/WebViewClient#ERROR_AUTHENTICATION



Related Topics



Leave a reply



Submit