0% found this document useful (0 votes)
48 views15 pages

Progressbar Component

This document discusses using a ProgressDialog in Android to display a progress bar. It explains that ProgressDialog allows creating a progress bar by instantiating it as an object. Various properties of the progress bar like style, text, and determinate/indeterminate state can be set. Methods like getMax(), incrementProgressBy(), setMax(), setProgress() are provided to control the progress bar. An example app is given that displays a horizontal progress bar on a button click by running a thread to increment the progress over time.

Uploaded by

Om
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views15 pages

Progressbar Component

This document discusses using a ProgressDialog in Android to display a progress bar. It explains that ProgressDialog allows creating a progress bar by instantiating it as an object. Various properties of the progress bar like style, text, and determinate/indeterminate state can be set. Methods like getMax(), incrementProgressBy(), setMax(), setProgress() are provided to control the progress bar. An example app is given that displays a horizontal progress bar on a button click by running a thread to increment the progress over time.

Uploaded by

Om
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Android Progress Bar using

ProgressDialog
Progress bars are used to show progress of a task. For example, when
you are uploading or downloading something from the internet, it is
better to show the progress of download/upload to the user.
In android there is a class called ProgressDialog that allows you to
create progress bar. In order to do this, you need to instantiate an
object of this class. Its syntax is.
ProgressDialog progress = new ProgressDialog(this);
Now you can set some properties of this dialog. Such as, its style, its
text etc.
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
Apart from these methods, there are other methods that are provided
by the ProgressDialog class
1. getMax()
This method returns the maximum value of the progress.
2 incrementProgressBy(int diff)
This method increments the progress bar by the difference of value
passed as a parameter.
3 setIndeterminate(boolean indeterminate)
This method sets the progress indicator as determinate or
indeterminate.
4 setMax(int max)
This method sets the maximum value of the progress dialog.
5. setProgress(int value)
This method is used to update the progress dialog with some specific
value.
6 show(Context context, CharSequence title, CharSequence message)
This is a static method, used to display progress dialog.
Example
This example demonstrates the horizontal use of the progress dialog
which is in fact a progress bar. It display a progress bar on pressing the
button.
To experiment with this example, you need to run this on an actual
device after developing the application according to the steps below.
package com.example.sairamkrishna.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){
ProgressDialog 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();
}
}
<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="Tutorials Point"
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>
Let's try to run your application. We assume, you have connected your
actual Android Mobile device with your computer. To run the app from
Android studio, open one of your project's activity files and click Run
Eclipse Run Icon icon from the toolbar. Before starting your application,
Android studio will display following window to select an option where
you want to run your Android application.
Select your mobile device as an
option and then check your mobile
device which will display following
screen −
Just press the button to start the
Progress bar. After pressing,
following screen would appear −

It will continuously update itself.

You might also like