How to set a cancel button in Progress Dialog?

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.


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();

Tags:

Android