Sunday, September 7, 2014

How To Prevent The Default WebView Error Page and Show Alert Dialog in Android

Remove Web Page Not Available Default WebView Error Page in Android

public class MyWebViewClient extends WebViewClient {

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            //To Prevent  Web page not available
            if (errorCode == -2) {
            view.loadData("", "", null);
            //To Show Alert Dialog
//SplashScreenActivity.class is the Launcher Activity
 // In Case of Frament instead of Activity Replace ClassName.this and getApplicationContext() with getActivity()       
 
AlertDialog.Builder builder = new AlertDialog.Builder(ClassName.this);
            builder.setCancelable(false);
            builder.setTitle(Html.fromHtml("<font color='#7F02AE'><b>Star Cineplex</b></font>"));
            builder.setMessage(Html.fromHtml("<font color='#120049'>Your data services are not working.Please check your data services.</font>"));
            builder.setPositiveButton(Html.fromHtml("<font color='#7F02AE'><b>OK</b></font>"), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                       //SplashScreenActivity.class is your Launcher Activity
 // In Case of Fragment instead of Activity Replace getApplicationContext()  with getActivity()
                 
  Intent intent = new Intent(getApplicationContext(), SplashScreenActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("EXIT", true);
                        startActivity(intent);
            
                                  
                   
                }
            });
                   AlertDialog alert=builder.create();
            alert.show();

Now at OnCreate of the Launcher Activity(eg. SplashScreenActivity.class)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_image_mn);
        //To exit
        if (getIntent().getBooleanExtra("EXIT", false)) {
            SplashScreenActivity.this.finish();
            System.exit(0);
        }
To Get:      
      
                                       


In Stead Of:

                   

6 comments:

  1. hello can i get source code for this..?

    ReplyDelete
  2. this works fine
    but how can i add
    with Retry button .. :)
    thnx

    ReplyDelete
  3. I found this article easy to understand and very helpful. Can’t wait to see the other posts. Thank you for sharing!

    Melbourne App Developer

    ReplyDelete
  4. This does not work if there is a connection loss in the middle of the app.

    ReplyDelete