Can I show progress dialog and error message when parsing XML with JavaScript?

I am developing an android web app for a blog like a website. To do this, I display a list of HTML page content categories which, when clicked, shows articles related to that category.

I am pulling these articles from website rss feeds which are in XML format and using JavaScript. I am parsing it for display on an HTML page.

This XML parsing process takes a long time to load the page. During this period, I get a blank screen. I have implemented a progress dialog that works fine when the page is loaded the first time, but when the XML is processed by JavaScript, it doesn't appear.

This is how I applied the Process dialog. Activity.java:

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
      this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.main);

        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setBackgroundColor(0);

        final ProgressDialog progressDialog = new ProgressDialog(activity);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Loading...please wait");
        progressDialog.setCancelable(true);
        webview.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }

        });

        webview.loadUrl("file:///android_asset/HomePage.html");

        // WebChromeClient give progress etc info
        webview.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress)
            {
                progressDialog.show();
                progressDialog.setProgress(0);
                activity.setProgress(progress * 1000);
                progressDialog.incrementProgressBy(progress);
                if (progress == 100 && progressDialog.isShowing())
                    progressDialog.dismiss();
            }
        });
}

      

  • How can I show the progress dialog when XML is being processed by JavaScript?
  • Also I want to show an error message if there is no internet connectivity for the same thing - is there any way to do this?
  • I used a call function like "tel: phone number" which worked but after I added the public boolean shouldOverrideUrlLoading doesn't work? what I did wrong?
+3


source to share


1 answer


for your questions you can use the following code in your activity.java file

   package com.package name;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
public class Myactivity extends Activity {
    TextView myLabel;
    WebView wv;
    final Activity activity=this;
    /** Called when the activity is first created. */





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.main);

        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);

        wv=(WebView)findViewById(R.id.webview);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setBackgroundColor(0);

        final ProgressDialog progressDialog = new ProgressDialog(activity);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Loading...please wait");
        progressDialog.setCancelable(true);
        wv.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                 if (url.startsWith("tel:")) { 
                     Intent intent = new Intent(Intent.ACTION_DIAL,
                             Uri.parse(url)); 
                     startActivity(intent); 
                     return true;
             }else{
                view.loadUrl(url);
                return true;
            }

            }
        });

        wv.loadUrl("file:///android_asset/page.html");

        // WebChromeClient give progress etc info
        wv.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress)
            {
                progressDialog.show();
                progressDialog.setProgress(0);
                activity.setProgress(progress * 1000);
                progressDialog.incrementProgressBy(progress);
                if (progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
            }
        }); 

        if (AppStatus.getInstance(this).isOnline(this)) {

            Toast t = Toast.makeText(this,"Welcome !!!!",8000);
            t.show();
            } else {  
                AlertDialog alertDialog = new AlertDialog.Builder(
                        CafeNashikActivity.this).create();

        // Setting Dialog Title
        alertDialog.setTitle("No Internet");

        // Setting Dialog Message
        alertDialog.setMessage("Internet Connection not available!");

        // Setting Icon to Dialog
        //alertDialog.setIcon(R.drawable.tick);

        // Setting OK Button
        alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    MyActivity.this.finish();

                }
        });

        // Showing Alert Message
        alertDialog.show();  
            }


    }


}

      



Hope this helps you.

+2


source







All Articles