OnkarDhane
OnkarDhane

Reputation: 1460

Showing time on ProgressBar in Android

Now,i have created progress bar and i want to allot time for progress bar(Ex. 2min) and user must able to see time while running progress bar. how we can achieve this?

Upvotes: 1

Views: 8888

Answers (3)

JAL
JAL

Reputation: 3319

Onkar... AsyncTask can both host the time intensive task and report progress, IF the time intensive task can be broken up into sub-tasks. AsyncTask uses generics to accomplish its magic. The progress type would be the middle type of the three "required formal type parameters" being <Params,Progress,Result>.

JAL

Upvotes: 0

SteD
SteD

Reputation: 14027

You can update your ProgressBar using CountDownTimer

    bar = (ProgressBar) findViewById(R.id.progress);
    bar.setProgress(total);
    int twoMin = 2 * 60 * 1000; // 2 minutes in milli seconds

    /** CountDownTimer starts with 2 minutes and every onTick is 1 second */
    cdt = new CountDownTimer(twoMin, 1000) { 

        public void onTick(long millisUntilFinished) {

            total = (int) ((dTotal / 120) * 100);
            bar.setProgress(total);
        }

        public void onFinish() {
             // DO something when 2 minutes is up
        }
    }.start();

Upvotes: 5

Shashank_Itmaster
Shashank_Itmaster

Reputation: 2475

I suggest you to use progress dialog rather using progress bar.Again for setting it is also possible using thread in progress dialog.

Please check this link.Also link from official android blog. Link Please see Creating Progress Dialog & in that second example using thread.

Upvotes: 1

Related Questions