Android Progress Bar

0
206

A Progress Bar is used to measure the progress of an ongoing task. For an instance, you can see the upload or download of an element to the or from the Internet, respectively. That’s where a progress bar comes into existence.

Also Read: Android RadioGroup Control

Various Methods Provided by the ProgressDialog Class
The creation of a progress bar in Android is only possible with the help of the ProgressDialog class. For that, instantiate an object of the ProgressDialog class and then you are good to proceed ahead. In order to do this, follow the given syntax:

ProgressDialog progress = new ProgressDialog(this); 

Now, you can easily set the required properties for your dialog. It may range from setting a message to a specific style.

progress.setMessage(“Downloading Music 🙂 “);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);

There are a few important and most commonly used methods offered by the ProgressDialog class. We have listed them out in the table provided below.

S. No. Method Description
1 getMax() * This method retrieves or returns the max value of the Progress Dialog.
2 setMax(int max) * This method sets the max value for the Progress Dialog.
3 setProgress(int value) * When you are required to update the Progress Dialog with some particular values then this method is taken into consideration.
4 incrementProgressBy(int diff) * This method is used to increment the progress bar by the diff value passed to it.
5 setIndeterminate(boolean indeterminate) * The progress indicator can either be set as determinate or indeterminate by using this method.
* It accepts either true or false.
6 show(Context context, CharSequence title, CharSequence message) * This is a static method.
* This method is used to display the Progress Dialog.

The following example will help you to include a progress bar in your application. The display of the progress bar will only be possible once the user clicks on the Button provided on the screen. Here, you will get a step-to-step guide to develop an Android application with the provision of a progress bar.

Step 1: Create an Android application with the name myapplication. This is to be done on the Android Studio IDE. Store this app under the com.example.w3school.myapplication package.

Step 2: Now, update the given code in the MainActivity.java file. To find this file, look into the src/ folder. In this file, you are adding progress code as to display the progress dialog on the screen. Main activity file also contains the fundamental lifecycle callback methods as its default content.

package com.example.w3school.myapplication;
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; 

public class MainActivity extends ActionBarActivity
{
   Button b1;
   private ProgressDialog progress;   
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1 = (Button) findViewById(R.id.button2);
   }   

   public void download(View view)
   {
      progress=new ProgressDialog(this);
      progress.setMessage(“Downloading Music”);
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(true);
      progress.setProgress(0);
      progress.show();      
      final int totalProgressTime = 100;
      final Thread t = new Thread()
      {
         @Override
         public void run()
         {
            int jumpTime = 0;            
            while(jumpTime < totalProgressTime)
            {
               try
               {
                  sleep(200);
                  jumpTime += 5;
                  progress.setProgress(jumpTime);
               } catch (InterruptedException e)
               {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
         }
      };
      t.start();
   }
} 

Step 3: Another file which needs modification is activity_main.xml. Add the following XML code to this file. You can easily identify this file after following the res/layout/ directory. 

<RelativeLayout xmlns: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:paddingLeft=”@dimen/activity_horizontal_margin”
   android:paddingRight=”@dimen/activity_horizontal_margin”
   android:paddingTop=”@dimen/activity_vertical_margin”
   android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>   

   <TextView
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/textView”
      android:layout_alignParentTop=”true”
      android:layout_centerHorizontal=”true”
      android:textSize=”30dp”
      android:text=”Progress bar” />      

   <TextView
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”W3School”
      android:id=”@+id/textView2″
      android:layout_below=”@+id/textView”
      android:layout_centerHorizontal=”true”
      android:textSize=”35dp”
      android:textColor=”#ff16ff01″ />      

   <Button
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Download”
      android:onClick=”download”
      android:id=”@+id/button2″
      android:layout_marginLeft=”125dp”
      android:layout_marginStart=”125dp”
      android:layout_centerVertical=”true” />      
</RelativeLayout>

Step 4: Let the AndroidManifest.xml file remain as it is.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.w3school.myapplication” >
   <application
      android:allowBackup=”true”
      android:icon=”@mipmap/ic_launcher”
      android:label=”@string/app_name”
      android:theme=”@style/AppTheme” >   

      <activity
         android:name=”.MainActivity”
         android:label=”@string/app_name” >      
         <intent-filter>
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />
         </intent-filter>      
      </activity>   
   </application>
</manifest>

Step 5: Now, run this app in the Android Studio. For that, you will be required to have an AVD (must have created during the environment setup). Open one of the activity files from your project and click on the Run button. Before the process starts, you will get the following dialog box, asking you to choose one of the available options.

Select your mobile device and you will be displayed with the final output on your device. Now, the screen is having a “DOWNLOAD” button to offer. Once the user clicks on it, he/she will be displayed with a progress bar labeled as “Downloading Music”.