2

I'm having a bad time with "ProgressThings". I have an app that dynamically changes the contents of the Views (and even the Views themselves might change, disappear, be re-added, so on). There are some classes that takes care of particular "styles" of the main Activity. One of them downloads a XML file, parses it (there is a specific class to handle this) and show some content to the user based on what was downloaded.

Anyway, my problem is that I wanted the ProgressDialog to be non blocking, so that the user could interact with other app's features while waiting. I have read some answers related to this problem (like this one, for example), but I have not been successful so far. Changing to ProgressBar with the "setIndeterminate(true)" (for I want the spinning style) doesn't seen to be working (it simply doesn't appear).

The private class bellow is the one that I use to show the ProgressDialog. It is working the way I posted bellow, but it is blocking UI interactions =/.

private class DownloadXmlTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog mProgress;

    private int xmlType = 0;
    XmlHandler xmlHandler = null;

    DownloadXmlTask(){
        xmlHandler = new XmlHandler();
    }

    @Override 
    protected void onPreExecute(){

        mProgress = new ProgressDialog(context);
        mProgress.getWindow().setGravity(Gravity.CENTER);
        mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgress.setIndeterminate(true);
        mProgress.setMessage("Carregando...");
        mProgress.show();
    }

    @Override
    protected Boolean doInBackground(String... urls) {

        int count = urls.length;
        for(int i=0; i<count; i++){
            parseXml(urls[i]);
        } 
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(result == true){
            mProgress.dismiss();
            // Do something else

        }
    }

    /**
     * Parses the xml defined by the parameter's String
     * @param urlString The url's String
     */
    private void parseXml(String urlString){
        try {
            // Loads the url
            URL url = new URL(urlString);

            // Initializes the parser
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            // Sets the handler
            XMLReader xr = sp.getXMLReader();
            xr.setContentHandler(xmlHandler);

            // Parses
            xr.parse(new InputSource(url.openStream())); 

        }catch(Exception e) {
            // To something here
        }
    }
}

P.S.: I didn't put the code for the XmlHandler class for it is not necessary ;P

P.S.2: Thanks in advance for any help :D

Community
  • 1
  • 1
Alesqui
  • 6,417
  • 5
  • 39
  • 43
  • But you definitely need `ProgressBar` instead of `ProgressDialog`. – ernazm Aug 16 '11 at 15:12
  • Yeah... And I tried to use the `ProgressBar` instead of `ProgressDialog`, but like I said the progress simply isn't appearing. I've tried a handful of possibilities with the `ProgressBar`... I really must be doing something wrong. I tried something like: `mProgress = new ProgressBar(context); mProgress.setIndeterminate(true); mProgress.setVisibility(View.VISIBLE);`, and when the job is finished I used `mProgress.setVisibility(View.GONE);`, but it doesn't show. And since it's just a spinning progress, I don't think I'll need something like the AsyncTask's `publishProgress`. – Alesqui Aug 16 '11 at 17:20

2 Answers2

2

As I said you need ProgressBar instead. The reason why it doesn't work for you is that ProgressBar is a View, and like the other View it has to be added to some parent to be shown. Refer to some tutorial and pay attention to the xml layout.

ernazm
  • 9,208
  • 4
  • 44
  • 51
  • Thank you very much Ernazm! It worked! I misunderstood things here with all the dynamic changes happening in my layout and just skipped the "adding view" part. And the fact that the ProgressDialog doesn't need to be added in any layout made me fall to this mistake. Thank you again for the the time spent helping me – Alesqui Aug 17 '11 at 12:00
0

You are currently showing ProgressDialog on onPreExecute() which means your UI thread will be blocked by the Dialog. I suggest you use ProgressBar like ernazm mentioned and post the progress of AsyncTask's background job by using onProgressUpdate(). You can put the ProgressBar on the application's titlebar like this thread on SO

Community
  • 1
  • 1
romy_ngo
  • 1,061
  • 8
  • 15
  • Unfortunately the answer in the thread that you pointed doesn't work for me, because my app must not have the default tittle (and so I am using `Window.FEATURE_NO_TITLE`). Thanks for the info though – Alesqui Aug 16 '11 at 17:17
  • @Alesqui How about adding a fixed `ProgressBar` into your layout View, for example on top of the layout but with `View.Gone`. When you want to use it, set the visibility to `View.Visible` and post the progress of `AsyncTask` to the `ProgressBar` by using `onProgressUpdate()` – romy_ngo Aug 17 '11 at 02:40
  • That seems to work just fine too romy_go. I'll just mark the ernazm's answer for it works better for me (since I've already tried and its already working now :D) – Alesqui Aug 17 '11 at 12:02