I want a progressbar but get a spinner progressdialog
Asked Answered
C

4

9

I am using a public AsynTask to download data, and I am trying to show a progress bar which would show the download progress. I think I have the code right, but all I get is a spinner progressdialog. Am I missing something? Why isn't the progress bar showing up? Here is the code. Thanks for any pointers.

public class FileDownloader extends AsyncTask<String, Integer, Void>
{
private Context _appContext;
private HttpURLConnection _urlConn;
private ProgressDialog _progressDia = null;
private DialogInterface.OnCancelListener _progDiaCancelListener = new DialogInterface.OnCancelListener()
{
    /**
     * When the progress dialog is canceled, stop the GET request.
     */
    public void onCancel(DialogInterface dialog) 
    {
        FileDownloader.this.cancel(true);
    }
};

/**
 * Constructor.
 * @param appContext
 */
public FileDownloader(Context appContext)
{
    _appContext = appContext;
    _progressDia = new ProgressDialog(_appContext, ProgressDialog.STYLE_HORIZONTAL);
    _progressDia.setMax(100);
    _progressDia.setTitle(_appContext.getString(R.string.diaHeader1));
    _progressDia.setMessage(_appContext.getString(R.string.diaBody1));
    _progressDia.setCancelable(true);
    _progressDia.setIndeterminate(false);
    _progressDia.setOnCancelListener(_progDiaCancelListener);
}

// Runs on the UI thread
@Override
protected void onPreExecute() 
{
         _progressDia.setProgress(0);
         _progressDia.show();                           
}

@Override
protected Void doInBackground(String... args) 
{
    String dloadURL = args[0],
           saveLoc = args[1];
            ...
            ...
        while((len = input.read(buf)) > 0)
        {
            output.write(buf, 0, len);
            total += len;
            publishProgress((int)total * 100/lenghtOfFile);
        }
            ...
            ...
    }
    catch(SocketTimeoutException ex)
    {   
    }
    finally
    {
         ...
}

// This is executed on main UI thread.
@Override
protected void onProgressUpdate(Integer... values) 
{
     _progressDia.setProgress(values[0]);
}

@Override
protected void onCancelled() 
{
         ...
}

// This is executed on main UI thread.
@Override
protected void onPostExecute(Void result) 
{
        removeProgressDialog();
        ...
}

    /**
     * Remove the message dialog, if still showing.
     */
    private void removeProgressDialog()
    {
        if(_progressDia != null && _progressDia.isShowing())
        _progressDia.dismiss();
    }
}
Capitalize answered 24/7, 2010 at 5:46 Comment(0)
R
8

Probably you forget to set dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

See this example code that works me:

ProgressDialog dialog;

@Override
protected void onPreExecute() {
    dialog = new ProgressDialog(this);
    dialog.setMessage("Matching progress");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setMax(100);
    dialog.setCancelable(false);
    dialog.show();
}

/*
 * (non-Javadoc)
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected Void doInBackground(Void... params) {

    return null;

}

protected void onPostExecute(Void result) {
    dialog.hide();
    dialog = null;
}
Rouault answered 24/7, 2010 at 8:0 Comment(2)
Thanks - this works. Only thing I had to change is use the single parameter constructor and set the progress style.Capitalize
Thanks, helped me too. My mistake was that I was calling ProgressDialog.show(...); and after that setting all other parametersSangfroid
A
4

It appears that you are using the two arg constructor for ProgressDialog, which the documentation suggests is for the Context (first arg) and the theme id (the second arg).

http://developer.android.com/reference/android/app/ProgressDialog.html#ProgressDialog(android.content.Context, int)

So while you THINK you are setting the ProgressStyle to STYLE_HORIZONTAL, you are not doing that at all, you are setting the theme id to something that is likely not a valid theme id.

I would suggest using the one arg constructor that takes in a Context, and then do as Pentium10 suggests and call _progressDia.setProgressStyle(ProgressStyle.STYLE_HORIZONTAL);

Arrowworm answered 24/7, 2010 at 17:4 Comment(1)
You are right - I thought that I am setting the style to horizontal by using the two param constructor, but that is not the case. Not sure what that is .... anyway, things are working now. Thanks.Capitalize
N
2

you can also give it this tag in the xml file

style="@android:style/Widget.ProgressBar.Horizontal"

Nabalas answered 24/7, 2010 at 23:51 Comment(1)
a million ups for this. the gui editor labels it as horizontal when you create it but doesn't create it as a horizontal progress bar(this is a bug in eclipse adk)Nannana
Y
-1

do entry of progress bar in your android project current intent XML file as a progress bar

also check it after saving in R.java file for id of your progress bar

the intent should be called whenever download is started

like

startActivity(new Intent(this, Progress.class));

Yawp answered 24/7, 2010 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.