Andy Stampor
Andy Stampor

Reputation: 688

Prevent multiple button presses on AlertDialog

I have a simple AlertDialog that is responding to multiple buttons being pressed at the same time. For instance, there is a Yes and a No button, and if the user uses two fingers to press both at once, both the yes and the no OnClickListeners are triggered and the code run. I've seen mention of android:splitMotionEvents="false", but am using just the default AlertBuilder without any added view and it doesn't appear that there is a way to set it. I could try something like creating a timestamp, but that seems like a hack and I would expect that there would be something in the Android code that would allow for that to be handled already.

Here is my code:

    AlertDialog.Builder builder = new AlertDialog.Builder(TestActivity.this);
builder.setTitle(R.string.title)

    .setMessage(getString(R.string.message))
    .setCancelable(false)
    .setPositiveButton(R.string.yes, new android.content.DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            runTest(false);
        }
    })
    .setNeutralButton(R.string.no, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            runTest(true);
        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // do nothing
        }
    });

AlertDialog dialog = builder.create();

Upvotes: 2

Views: 946

Answers (4)

Bala R
Bala R

Reputation: 108937

I have a feeling you have a style resource for this dialog

Just add

<item name="android:splitMotionEvents">false</item>

to your dialog style. It will prevent multi-touch for all dialogs using it.

Upvotes: 3

Mehmet K
Mehmet K

Reputation: 2814

Use a field variable like:

boolean isWorking = false;

Then integrate it like into all your onClickListeners to make sure they don't execute if another one is executing

...

.setPositiveButton(R.string.yes, new android.content.DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (!isWorking) {
            isWorking = true;
            runTest(false);
        }
    }
})

...

Upvotes: 2

Remario
Remario

Reputation: 3863

Easiest way is to declare a static variable , use this variable to track the pressed of the button you wish to resolve, and simply check if the current button pressed equals to the state stored.

Upvotes: 2

Anmol317
Anmol317

Reputation: 1376

it must be running one after another in the sequence the buttons were clicked. if you have completed the task of the alert dialog then u must close it on the button click and if you are doing some task on the click and dont want to close it then use the progressbar to unable the user to click on other buttons till the previous method is done.

Upvotes: 1

Related Questions