Progress unit in ProgressDialog
Asked Answered
L

4

9

Android's ProgressDialog allows you to set the current progress and maximum value as integers. These values are shown in the dialog like this:

3401/10023

Where the first number is the current progress, and the second number is the maximum value.

I would like to also show the unit of measurement. Something like this:

3401/10023 KB

Is this possible using ProgressDialog? If not, what do you recommend doing to give this information to the user? I'm trying to avoid reimplementing ProgressDialog just to include the unit.

Larger answered 16/8, 2010 at 9:37 Comment(3)
Why don't you do it in term of percentages ? Like 30/100Niblick
The progress dialog already shows the percentage. But in a file download progress dialog, I think it's more friendly to the user to show the progress in bytes, specially if it's a large file.Larger
If you take a look to ProgressDialog sources, the text is handled by a handler created in the onCreate method. See my next answerNiblick
V
5

Starting from API 11, you can call the following function to achieve your purpose.

mProgressDialog.setProgressNumberFormat("%1d/%2d kB") 
Vision answered 25/9, 2013 at 8:51 Comment(0)
L
6

Update: setProgressNumberFormat is part of the API since level 11.

The HEAD of the ProgressDialog source code already includes a public function called setProgressNumberFormat which can be used to set the unit. Unfortunately this function does not seem to be available in the latest Android version. I guess it will be included in a future update.

In the meantime, copying this implementation of ProgressDialog is the best option. Subclassing ProgressDialog is of no use because all of its members are private and working with view.findViewById(R.id.progress_number) to get the TextView directly is extremely risky, as nothing ensures that the id will be always the same (or that the TextView will always exist).

Larger answered 16/8, 2010 at 20:8 Comment(2)
Yep but at least 2.3 for the setProgressNumberFormat. The findViewById is not recommended at all, nothing tells you that any actual or future version has the same idNiblick
Agreed. I actually mentioned those possibilities to discourage their use, but I will make it clearer.Larger
N
5

In the ProgressDialog source file :

mViewUpdateHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                /* Update the number and percent */
                int progress = mProgress.getProgress();
                int max = mProgress.getMax();
                double percent = (double) progress / (double) max;
                mProgressNumber.setText(progress + "/" + max);
                mProgressPercent.setText(mProgressPercentFormat.format(percent));
            }
        };

You must reimplement it, you cannot avoid it

Niblick answered 16/8, 2010 at 19:26 Comment(0)
V
5

Starting from API 11, you can call the following function to achieve your purpose.

mProgressDialog.setProgressNumberFormat("%1d/%2d kB") 
Vision answered 25/9, 2013 at 8:51 Comment(0)
H
3

It wasn't obvious for me that I can just set:

mProgressDialog.setMessage("Downloading...(size in kB)");

Maybe this simplest way isn't obvious for somebody else...

Hitlerism answered 7/12, 2012 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.