Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Progress bar while downloading

I want to show a progress bar showing percentage completed while my app is downloading some big files over the internet. something like this :enter image description here

like image 254
Hieu Le Avatar asked Dec 20 '13 02:12

Hieu Le


3 Answers

xml file code:

 <ProgressBar
        android:id="@+id/xml_reg_progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/common_left_right_margin"
        android:layout_marginRight="@dimen/common_left_right_margin"
        android:layout_marginTop="@dimen/reg_ph_linear_top_margin"
        />

you can change progressbar style as per your requirement.

this is java file code:

 protected static final int TIMER_RUNTIME = 180000; // in ms --> 10s
    private ProgressBar progressTimer;
    public void startTimer() {
            final Thread timerThread = new Thread() {
                @Override
                public void run() {
                    mbActive = true;
                    try {
                        int waited = 0;
                        while (mbActive && (waited < TIMER_RUNTIME)) {
                            sleep(1000);
                            if (mbActive) {
                                waited += 1000;

                                updateProgress(waited);
                            }
                        }
                    } catch (InterruptedException e) {
                        // do nothing
                    } finally {
                        onContinue();
                    }
                }

            };
            timerThread.start();

        }

        Handler handler = new Handler();

        private void onContinue() {
            handler.post(new Runnable() {
                @Override
                public void run() {

                    txtCallContactUsNumber
                            .setText(CommonVariable.CallForSupporMessage
                                    + contactInfo.MobileNo);

                }
            });

        }

        public void updateProgress(final int timePassed) {
            if (null != progressTimer) {
                // Ignore rounding error here
                final int progress = progressTimer.getMax() * timePassed
                        / TIMER_RUNTIME;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("timePassed", "timePassed=" + timePassed);

                        DateFormat formatter = new SimpleDateFormat("mm:ss");
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(timePassed);
                        String timer = formatter.format(calendar.getTime());
                        formatter.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
                        txtTimerCount.setText(formatter.format(timePassed));
                    }
                });

                progressTimer.setProgress(progress);
            }
        }

in my code this will call 1 second and get progress increment every 1 second.

like image 102
dipali Avatar answered Oct 28 '22 13:10

dipali


You can acheive this by using a progress bar in two ways.

1.Add a progress bar(the circle one) to act as a loading indicator.

To do this simple show a progress bar on start of the image download thread progressbar.show(); After the download is finished dismiss the progressbar progressbar.dismiss();

You can use AsyncTask for this

Refer this link

2.Add a progress bar(the rectangular bar type) to show the progress of the download or what ever it may be.

I would recomend you to go through this link..

like image 27
Abx Avatar answered Oct 28 '22 13:10

Abx


Check these links:

http://examples.javacodegeeks.com/android/core/ui/progressdialog/android-progressdialog-example/

Show ProgressDialog Android

Clearly, explains how to implement the ProgressBar or ProgressDialog

Here's a good library for customized ProgressDialog:

https://github.com/passsy/android-HoloCircularProgressBar

https://github.com/Todd-Davies/ProgressWheel

like image 29
Chintan Soni Avatar answered Oct 28 '22 12:10

Chintan Soni