Usama Sarwar
Usama Sarwar

Reputation: 9020

avoid multiple clicks on a button

I have an application where in which i have two buttons, named 'next' and 'previous'. These buttons are to navigate through the stories being loaded from a url. When the button is tapped the dialog appears to show "Please wait loading data". When we tapp the next or previous button quite extravagantly, a never ending dialog appears on screen.

Am doing it this way:

public void onClick(View v) {

    try {
        pDialog = new ProgressDialog(StoriesListController.this);
        pDialog.setIndeterminate(true);
        pDialog.setIcon(R.drawable.icon_small);
        pDialog.setCancelable(true);
        if (isFavoriteList == true) {
            pDialog.setMessage("Loading favorites...");
        } else {
            pDialog.setMessage("Loading data...");
        }
        pDialog.setTitle("Please wait");
        pDialog.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface arg0) {
                if (cancelableHeavyWorker != null) {
                    cancelableHeavyWorker.setHandler(null);
                }
                finish();
            }
        });
        pDialog.show();

        if (v == next) {
            if (ind < items.size() - 1) {   
                ind++;
                loadAndShowNextActivity();
            }
        } else if (v == previous) {
            if (ind > 0) {
                ind--;
                loadAndShowNextActivity();
            }
        }

    } catch (Exception e) {

        Log.e("Error", "--.OnClink Message" + e.toString());
        e.printStackTrace();
    }
}

Where am i going wrong. How can i avoid multiple clicks or avoid never ending dialog. ANy help is appreciated.

Upvotes: 2

Views: 3670

Answers (5)

Georgy Gobozov
Georgy Gobozov

Reputation: 13721

dialogButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
                dialogButton.setEnabled(false);
                ....



 dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    dialogButton.setEnabled(true);
                }
            });

Upvotes: 0

Aracem
Aracem

Reputation: 7207

After click put the button setEnabled(false) and in the loadAndShowNextActivity() dismiss the pDialog.dismiss()

Upvotes: 0

Jeroen
Jeroen

Reputation: 4023

Alternatively to the other answers you could set a boolean variable to true when the button is clicked and set it to false when you're done processing the click.

This way you can ignore multiple clicks and not having to disable the button possibly avoiding annoying flickering of the button.

Upvotes: 2

peter.bartos
peter.bartos

Reputation: 12045

after clicking the button, do setEnabled(false) to this button, to disable multiple clicks

Upvotes: 3

Tobias
Tobias

Reputation: 7380

Disable "next" button until the data is loaded completly or the process is canceled.

Upvotes: 3

Related Questions