Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a cancel button in Progress Dialog?

Tags:

android

I want to set a cancel button in my ProgressDialog. Below is my code:

myDialog = new ProgressDialog(BaseScreen.this); myDialog.setMessage("Loading..."); myDialog.setCancelable(false); myDialog.show(); 

I want to set a button with an onClickListener on this ProgressDialog. I tried with this code:

myDialog.setButton("Cancel", new OnClickListener() {             @Override     public void onClick(DialogInterface dialog, int which) {         // TODO Auto-generated method stub                   myDialog.dismiss();     } }); 

But it isn't working. I tried other similar listeners also, but still no success. How can I solve this problem?

like image 287
Gaurav Gupta Avatar asked Aug 12 '11 08:08

Gaurav Gupta


People also ask

How do I cancel my progress dialog?

setCancelable(false); myDialog. setButton(DialogInterface. BUTTON_NEGATIVE, "Cancel", new DialogInterface. OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.

How to close Progress dialog in android?

OnCancelListener(){ @Override public void onCancel(DialogInterface dialog){ /****cleanup code****/ }}); The setCancelable method tells the ProgressDialog to close when the back button is pressed. The listener at the end allows you to do anything that may need to be done as a result of a cancel (like closing a socket).

How do I stop progress bar?

You want to stop the progressDialog or change its UI not to be circular? You can set your progressbar's visibility to invisible or gone through progressbar. setVisibility(View. INVISIBLE); or progressBar.

What can I use instead of progress dialog?

ProgressBar is best alternative for ProgressDialog.


2 Answers

Make sure you call myDialog.setButton before calling myDialog.show();
Also you can use myDialog.setButton("Cancel", (DialogInterface.OnClickListener) null); if you only need to close the dialog on button click.

like image 32
ernazm Avatar answered Oct 09 '22 21:10

ernazm


The setButton method you are using is deprecated (although it should still work). Also, you might want to add the button before showing the dialog. Try:

myDialog = new ProgressDialog(BaseScreen.this); myDialog.setMessage("Loading..."); myDialog.setCancelable(false); myDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {     @Override     public void onClick(DialogInterface dialog, int which) {         myDialog.dismiss();//dismiss dialog     } }); myDialog.show(); 
like image 93
Felix Avatar answered Oct 09 '22 22:10

Felix