Introduction of Android ProgressBar Control
Situation : If someone drove bike. Bike has speedometer. This show bike speed. as we accelerate bike speedometer also show relative speed.
Conclusion : Speedometer shows progress of bike speed. Same thing or concept we apply with android progressbar. This progress bar shows our task progress.
Progress bar always used for long time running task in android. So that user aware about this.
Conclusion : Speedometer shows progress of bike speed. Same thing or concept we apply with android progressbar. This progress bar shows our task progress.
Progress bar always used for long time running task in android. So that user aware about this.
How to use progress bar
- Make object of progressbar.
ProgressDialog progressBar=new ProgressDialog(this);
- Then set properties for this progressbar
progressBar.setCancelable(true); progressBar.setMessage("File downloading ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); - Start Background thread that perform long running task.
new Thread(new Runnable() { public void run() { } }).start(); - Update progress bar status using progressBarHandler object. This object is created in following way.
Handler progressBarHandler = new Handler(); progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } });
Here progressBarStatus is integer variable which is updated as your task. - If long running task completed then dismiss progressbar
progressBar.dismiss();
Example- Make layout file which contains a button. If we click on button it show notification toast message.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download" /> </LinearLayout> - Then apply onclick event in mainactivity.class
public class MyAndroidAppActivity extends Activity { Button btnStartProgress; ProgressDialog progressBar; private int progressBarStatus = 0; private Handler progressBarHandler = new Handler(); private long fileSize = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { btnStartProgress = (Button) findViewById(R.id.btnProgress); btnStartProgress.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // prepare for a progress bar dialog 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(); //reset progress bar status progressBarStatus = 0; //reset filesize fileSize = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { // process some tasks progressBarStatus =Tasksfunction(); // your computer is too fast, sleep 1 second try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // Update the progress bar progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } // ok, file is downloaded, if (progressBarStatus >= 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 progressBar.dismiss(); } } }).start(); } }); } // file download simulator... a really simple public int Tasksfunction() { while (fileSize <= 1000000) { fileSize++; if (fileSize == 100000) { return 10; } else if (fileSize == 200000) { return 20; } else if (fileSize == 300000) { return 30; } // ...add your own } return 100; } } - Output :
- Make layout file which contains a button. If we click on button it show notification toast message.

This comment has been removed by the author.
ReplyDelete