How to Embrace The Power of Android Progress Bar As a Usability Feature

0
Shares
How to Embrace The Power of Android Progress Bar As a Usability Feature
Rate this post

You might have noticed those bar filling itself when a task was in execution. Those bars are called progress bars. The Android progress bar is one of the common user interface component. It is used to show the progress of any task to the app users such as uploading, downloading, or to notify people that a task is running, how much task is left, or how much task has been finished.

In an Android app, there are many ways to implement Android progress bar feature, but here we’ll go through the simplest and most preferred way to show the progress of a running task.

Let’s Get Started

Create a new project under file menu and modify your project details.

ssprogressbardemo1

Choose Mini Support SDK in the next tab.

ssprogressbardemo2

Lastly, select Add No Activity and click on finish button.

ssprogressbardemo3

Now we’ll create a XML layout file in which, we’ll add one progress bar and one button. When the user presses the button, it will show how much task is left and how much task is completed in the Android progress bar.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="progressbar.com.progressbardemo.MainActivity"
android:weightSum="1">
<Button
android:id="@+id/btnStartProgress"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download File" />
</LinearLayout>

Now, we’ll create one class for MainActivity to define callback methods and represent a user interface within a single screen.

public class MainActivity extends AppCompatActivity {
ProgressDialog prgDialog;
private int prgStatus = 0;
private Handler prgBarHandler = new Handler();
private long fileSize = 0;
private Button btnStartPrg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartPrg = (Button) findViewById(R.id.btnStartProgress);
btnStartPrg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// prepare for a progress bar dialog
prgDialog = new ProgressDialog(view.getContext());
prgDialog.setCancelable(true);
prgDialog.setMessage("File downloading ...");
prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prgDialog.setProgress(0);
prgDialog.setMax(100);
prgDialog.show();
//reset progress bar status
prgStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (prgStatus < 100) {
// process some tasks
prgStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
prgBarHandler.post(new Runnable() {
public void run() {
prgDialog.setProgress(prgStatus);
}
});
}
// ok, file is downloaded,
if (prgStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
prgDialog.dismiss();
}
}

Now that your user interface is ready and callback methods have been implemented, it’s time that we run the demo.

ssprogressbardemo4

 

Worked just fine, didn’t it?

However, this was just a simple Android progress bar example, but it’s also possible to use this feature for various jobs. And in case you need help to implement it, hire Android app development company, layout a strategic plan, and start execution.

Grab a free copy of progressbar demo from Github.

This page was last edited on December 8th, 2016, at 16:37.
 
0
Shares
 

Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now