3

I am developing android web application for a blog like website. For this I am showing HTML page contenting list of categories which when clicked shows articles related to that category.

I am fetching this articles from website's RSS feeds which is in XML format and by using JavaScript I am parsing it to display on HTML page.

This process of parsing XML takes lot of time to load a page.During this period I am getting blank screen.I have implemented progress dialog which works fine when page is loading for the first time but when XML is getting parsed by JavaScript it does not appear.

Here is how I implemented 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();
            }
        });
}
  1. How can I show progress dialog while XML is being parsed by JavaScript?
  2. Also I want to show an error message if no internet connectivity available for same thing is their any way to do so?
  3. I have used call function as "tel:phone number" which was working but after I added de public boolean shouldOverrideUrlLoading it is not working? what's i done wrong?
Mario S
  • 11,715
  • 24
  • 39
  • 47
DoctorAV
  • 1,189
  • 1
  • 14
  • 40
  • Have you looked into AsyncTasks? It has a nice progress updater part in it that could help you out http://developer.android.com/reference/android/os/AsyncTask.html As for the second question take a look at this post http://stackoverflow.com/questions/4086159/checking-internet-connection-on-android – Katana24 Mar 29 '12 at 23:39

1 Answers1

2

for your questions You can use 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 will help you.

devloper
  • 244
  • 1
  • 2
  • 10