Reputation: 755
This has been asked in previous posts, but I've tried many and none of them seem to work. I have a boolean flag that should in theory prevent my dialog from being closed. Here is what I have so far:
boolean start_match = false;
public void WarmupDialog()
{
if(use_warmup == true)
{
final ProgressDialog spinner = new ProgressDialog(this);
spinner.setTitle("Warmup");
spinner.setCancelable(false);
spinner.setCanceledOnTouchOutside(false);
timer = new CountDownTimer(300000, 1000)//5 minutes
{
@Override
public void onFinish()
{
spinner.cancel();
}
@Override
public void onTick(long l)
{
spinner.setMessage(((int)Math.round(l/1000.0)-1)+"secs remaining of warmup");
}
};
spinner.setButton(DialogInterface.BUTTON_POSITIVE, "Start Warmup", (DialogInterface.OnClickListener)null) ;
spinner.show();
Button button = spinner.getButton(DialogInterface.BUTTON_NEUTRAL);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(start_match)
{
spinner.dismiss();
}
else
{
start_match = true;
Button button = spinner.getButton(DialogInterface.BUTTON_POSITIVE);
button.setText("Start Match");
timer.start();
}
}
});
spinner.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
timer.cancel();
ChooseServer();
}
});
}
else
{
ChooseServer();
}
}
This open a warm up timer for a tennis match. When the button is pressed for the first time the timer should start. When the button is pressed again the dailog should then cancel. At the moment the dialog is dismissed immediately. Any ideas? Thanks in advance for any help!
Upvotes: 1
Views: 89
Reputation: 5951
As far as my knowledge about Progress dialogs goes, it should be the normal behavior.
As ProgressDialog inherits from AlertDialog -> Dialog and by default they get dismissed when user press a button on the dialog, regardless of which button it is (positive/negative etc).
Suggestion: intercepting the touch event & detecting the source position of the touch event (i.e on which button user has touched), we should be able to override this behavior.
Upvotes: 1