below is the sample tutorial program to demonstrate a sample thread to display a progressbar in android.we just run the thread for a while and after that change the label to processing done we are using sleep here to emulate some useful operation just a add textview in the main.xml see the pic and below code increase the sleep value in case progress bar does not appear or closes too early to be viewed
public class ProgressDialogSample extends Activity implements Runnable {
private TextView txt;
private ProgressDialog progDailog;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
txt = (TextView) this.findViewById(R.id.main);
txt.setText("Press any key to start Process");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
progDailog = ProgressDialog.show(curContxt,
"Progress dialogue sample ", "ceveni.com please wait....",
true);
new Thread() {
public void run() {
try{
// just doing some long operation
sleep(5000);
} catch (Exception e) { }
handler.sendEmptyMessage(0);
progDailog.dismiss(); }
}.start();
}//
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
txt.setText("Processing Done");
}
};
}
we are using Handler because we get a across thread exception if we handle UI elements from other thread to overcome this problem we can post to the handler once the job / processing is completed so that UI thread can execute the ui operations once the processing gets on the thread gets completed.
15 comments:
I want to move from the progressbar page to another.for example-- when the progress bar stops it takes me to some another page where i wanna go...
Thanks in advance
From
Jimmy
Thank u ..... Its actually a very nice tutorial for a beginner like me in threads......
@Harminder Create an activity in the Handler , This is one of the solution for ur question.....
Thanks sooo much.. I was having a lot of trouble getting the dialog to display before the code finished executing. This worked out perfectly.
PERFECT! I tried numerous thread/handler examples to no avail. This did the trick!
i have problem with curContxt,why it show error?
try getApplicationContent() for curContxt
Great solution, thanks a lot :)
keep up the good work :-)
how to stop progress bar(after 30 seconds)
could u please tell me
how to stop progress bar(after 30 seconds)
could u please tell me
Thanks Man!! Nice solution !! handle is nice way to perform additional tasks!
The type ProgressDialogSample must implement the inherited abstract method Runnable.run()
It wont compile ?????
I don't get why ppl are praising it so much. It has too many errors to even compile, let alone run..
Your code is very useful for me.Thank you
very nice tutorial you can also check this onehttp://pavanhd.blogspot.in/2013/04/android-progress-dialog-example.html>
Post a Comment