Android Tutorial on Progress Bar File Downloading

Progress Bar File Download :

In day to day we use many applications where sometimes we may download few files for which a Progress Bar is to be displayed showing the status of the file.

Not only file downloading but in several other process which run in background displays a message in terms of a progress bar, we will be learning this in Android Tutorial on Progress Bar File Download.

In general you may see a youtube video and there you can notice video loading using a progress bar which is in circular shape such that we will have to wait until the video is loaded.

Generally in application startup we see a loading… kind of status which will be loading based on percentage basis.This will indicate the amount of work done in background.

 

In this tutorial i am showing you a horizontal progress bar which will look like

loading

 

Progress Bar File Download Video:

MainActivity.java

We need to add a button to start downloading process. Then we need to initialize Progress Bar.

Also specify the progress style, starting value and ending as well.Finally you need to add show() method to display progress bar.

 

progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File Downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();

 

Here in next step we will make a step by step increasing of progress bar by returning the percentages as specified below.

 

while (level <= 100) {

    level++;

    if (level == 10) {
        return 10;                       //10 percent
    } else if (level == 20) {
        return 20;                       //20 percent 
    } else if (level == 30) {
        return 30;
    } else if (level == 40) {
        return 40;
    } else if (level == 50) {
        return 50;
    } else if (level == 60) {
        return 60;
    } else if (level == 70) {
        return 70;
    } else if (level == 80) {
        return 80;
    } else if (level == 90) {
        return 90;                    //90 percent
    }


}

return 100;

Full Code :

Providing the full code for Progress Bar File Download. You can set the status message while downloading a file and also after the file is successfully downloaded.

 

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    Button startDownload;
    ProgressDialog progressBar;
    private int progressBarStatus = 0;
    private Handler progressBarHandler = new Handler();

    private long level = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startDownload = (Button) findViewById(R.id.startDownload);

        startDownload.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                progressBar = new ProgressDialog(v.getContext());
                progressBar.setCancelable(true);
                progressBar.setMessage("File Downloading ...");
                progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressBar.setProgress(0);
                progressBar.setMax(100);
                progressBar.show();

                progressBarStatus = 0;


                level = 0;

                new Thread(new Runnable() {
                    public void run() {
                        while (progressBarStatus < 100) {

                            progressBarStatus = progresslevel();

                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            progressBarHandler.post(new Runnable() {
                                public void run() {
                                    progressBar.setProgress(progressBarStatus);

                                }
                            });
                        }

                        if (progressBarStatus >= 100) {


                            try {
                                Thread.sleep(2000);

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            runOnUiThread(new Runnable() {
                                public void run() {


                                    new AlertDialog.Builder(MainActivity.this)
                                            .setTitle("File Download")
                                            .setMessage("File Downloaded Successfully!!!")
                                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int which) {

                                                }
                                            })

                                            .show();

                                }
                            });
                            progressBar.dismiss();
                        }
                    }
                }).start();

            }

        });


    }



    public int progresslevel() {

        while (level <= 100) {

            level++;

            if (level == 10) {
                return 10;
            } else if (level == 20) {
                return 20;
            } else if (level == 30) {
                return 30;
            } else if (level == 40) {
                return 40;
            } else if (level == 50) {
                return 50;
            } else if (level == 60) {
                return 60;
            } else if (level == 70) {
                return 70;
            } else if (level == 80) {
                return 80;
            } else if (level == 90) {
                return 90;
            }


        }

        return 100;

    }

}

 

activity_main.xml

Here is the design layout file where we will add a button that will demonstrate the Progress Bar File Download.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:id="@+id/startDownload"
        android:layout_width="250dp"
        android:layout_height="100dp"
        android:text="Click to download a file" />

</LinearLayout>

Output :

The screen below depicts Android Progress Bar File Download

Android Progress Bar File Download Android Progress Bar File Download Android Progress Bar File Download Android Progress Bar File Download

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If you have any query’s in this tutorial on Progress Bar File Download do let us know in the comment section below and if you like this tutorial do share and like for more android tutorials visit our website.

 

 

Show Buttons
Hide Buttons
Read previous post:
Android Fetch Current Latitude and Longitude Values

  Android Fetch Latitude Longitude : In General few apps require user location, there we can enter address in a...

Close