Search

Tuesday, September 1, 2009

simple progress bar dialog in android with thread

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:

Unknown said...

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

Nithya said...

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.....

Steven McWhorter said...

Thanks sooo much.. I was having a lot of trouble getting the dialog to display before the code finished executing. This worked out perfectly.

Gio said...

PERFECT! I tried numerous thread/handler examples to no avail. This did the trick!

Anonymous said...

i have problem with curContxt,why it show error?

Anonymous said...

try getApplicationContent() for curContxt

Anonymous said...

Great solution, thanks a lot :)

Anonymous said...

keep up the good work :-)

venky said...

how to stop progress bar(after 30 seconds)
could u please tell me

venky said...

how to stop progress bar(after 30 seconds)
could u please tell me

Usman said...

Thanks Man!! Nice solution !! handle is nice way to perform additional tasks!

Anonymous said...

The type ProgressDialogSample must implement the inherited abstract method Runnable.run()

It wont compile ?????

Unknown said...

I don't get why ppl are praising it so much. It has too many errors to even compile, let alone run..

Anonymous said...

Your code is very useful for me.Thank you

Pawan said...

very nice tutorial you can also check this onehttp://pavanhd.blogspot.in/2013/04/android-progress-dialog-example.html>

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google