Android Progressbar

Explain the use of progressbar.

The main use of progressbar is showing the progress of current ongoing task.
For example, if you are uploading or downloading some files from the internet, then you can use progressbar to show the progress of task to the user.

To add a progress bar to a layout file (activity_main.xml), you can use the <ProgressBar> element or you can directly drag progressbar control from control palette. By default, the progress bar is a spinning wheel, but you can change it. To change to a horizontal progress bar, apply the Widget.ProgressBar.Horizontal style.

Android provides the following types of style for progressbar.
  • Widget.ProgressBar.Horizontal
  • Widget.ProgressBar.Small
  • Widget.ProgressBar.Large
  • Widget.ProgressBar.Inverse
  • Widget.ProgressBar.Small.Inverse
  • Widget.ProgressBar.Large.Inverse
You can increment the progressbar status by using with incrementProgressBy() or setProgress() methods. By default, the progress bar maximum value is 100. You can change it by setting your own maximum value using android:max attribute.

Explain horizontal progressbar, while downloading the file.

In this example I have taken one ProgressBar, Button and textview control. I used thread for updating the value of the progress bar inside a thread. Thread.sleep(100) will show the progress slowly after incrementing the value.

Following is the content of res/layout/activity_main.xml file:
<RelativeLayoutxmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:paddingBottom = "@dimen/activity_vertical_margin"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
tools:context = ".MainActivity">

<Button
android:id = "@+id/button1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentTop = "true"
android:layout_marginLeft = "34dp"
android:layout_marginTop = "24dp"
android:text = "Download"/>

<TextView
android:id = "@+id/textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@+id/progressBar1"
android:layout_below = "@+id/progressBar1"
android:text = "TextView"
android:textSize = "20dp"/>

<ProgressBar
android:id = "@+id/progressBar1"
style = "@android:style/Widget.ProgressBar.Horizontal"
android:layout_width = "wrap_content"
android:layout_height = "40dp"
android:layout_alignParentLeft = "true"
android:layout_alignParentRight = "true"
android:layout_below = "@+id/button1"
android:layout_marginTop = "44dp"/>

</RelativeLayout>

Following is the content of the modified MainActivity.java file
publicclassMainActivityextends Activity
{
     privateProgressBarprogBar;
     private Handler handler = new Handler();;
     privateintProgressStatus = 0;
     private Button showProgressBar;
     TextViewtextView;
     @Override
     protectedvoidonCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          progBar = (ProgressBar)findViewById(R.id.progressBar1);
          textView = (TextView) findViewById(R.id.textView1);
          showProgressBar= (Button)findViewById(R.id.button1);
          showProgressBar.setOnClickListener(newView.OnClickListener()
          {
               @Override
               publicvoidonClick(View v)
               {
                     // TODO Auto-generated method stub
                    doSomeWork();
               }
          });
     }
     publicvoiddoSomeWork()
     {
          // Start lengthy operation by using thread
          new Thread(new Runnable()
          {
               publicvoid run()
               {
                    while (ProgressStatus < 100)
                    {
                         ProgressStatus += 1;
                         // Update the progress bar
                         handler.post(new Runnable()
                         {
                              publicvoid run()
                              {
                                   progBar.setProgress
                                   (ProgressStatus);
                                   textView.setText(ProgressStatus+"/"+progBar.getMax());
                                   if(ProgressStatus==100)
                                   {
                                        Toast.makeText(getApplicationContext(), "Download completed", Toast.LENGTH_LONG).sho (); progBar.setVisibility(View.GONE);
                                   }
                              }
                         });
                         try
                         {
                              //Display progress slowly
                              Thread.sleep(100);
                         }
                         catch (InterruptedException e)
                         {
                              e.printStackTrace();
                         }
                    }
               }
          }).start();
     }
}

When you click on download button, the progress bar will show the progress of task.
Progress bar