Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Prevent multiple onClick events on a button (that has been disabled)

Tags:

A button triggers an action that should only be invoked once. The button is disabled and hidden in the onClick handler before the action is performed:

someButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        someButton.setEnabled(false);
        someButton.setClickable(false);
        someButton.setVisibility(View.GONE);
        performTaskOnce();
        }
    });

private void performTaskOnce() {
    Log.i("myapp", "Performing task");
    //Do something nontrivial that takes a few ms (like changing the view hierarchy)
}

Even though the button is disabled immediately, it is nonetheless possible to trigger multiple "onClick" events by tapping multiple times very quickly. (i.e. performTaskOnce is called multiple times). Is seems that the onClick events are queued before the the button is actually disabled.

I could fix the problem by checking in every single onClick handle whether the corresponding button is already disabled but that seems like a hack. Is there any better way to avoid this issue?

The problem occurs on Android 2.3.6, I cannot reproduce it on Android 4.0.3. But given the rarity of 4.x devices it is not an option to exclude older devices.

like image 208
rluba Avatar asked Jun 01 '12 09:06

rluba


People also ask

How do I stop a button from clicking multiple times android?

Sometimes user clicks button too fast or double time, if button performs some kind of network operation, it'll call the function multiple times. To prevent double click, you can record the last time button clicked, and compare it to threshold of desired time.

How does Android handle multiple click events?

Show activity on this post. call setClickable(false) for all buttons once one of them was clicked. call the next activity with startActivityForResult(...) override onActivityResult(...) and call setClickable(true) for all buttons inside it.

How do I turn off double tap on Android?

To turn off double tap on Android, locate the “Settings” app on your phone's home screen, and tap it. Then, press “Advanced Features” followed by “Motions and Gestures.” After which, swipe up until you locate “Double-Tap to turn off screen.” Tap on the toggle to turn it off.

What is setOnClickListener in Android?

OnClickListener and wires the listener to the button using setOnClickListener(View. OnClickListener) . As a result, the system executes the code you write in onClick(View) after the user presses the button. The system executes the code in onClick on the main thread.


2 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.

boolean processClick=true;
someButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if(processClick)
         {
        someButton.setEnabled(false);
        someButton.setClickable(false);
        someButton.setVisibility(View.GONE);
        performTaskOnce();
         }
        processClick=false; 
        }
    });

private void performTaskOnce() {
    Log.i("myapp", "Performing task");
    //Do something nontrivial that takes a few ms (like changing the view hierarchy)
}
like image 133
Vipul Avatar answered Sep 18 '22 10:09

Vipul


In the interest of keeping DRY:

// Implementation
public abstract class OneShotClickListener implements View.OnClickListener {
    private boolean hasClicked;

    @Override public final void onClick(View v) {
        if (!hasClicked) {
            onClicked(v);
            hasClicked = true;
        }
    }

    public abstract void onClicked(View v);
}

// Usage example
public class MyActivity extends Activity {
    private View myView;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myView.setOnClickListener(new OneShotClickListener() {
            @Override public void onClicked(View v) {
                // do clicky stuff
            }
        });
    }
}
like image 38
Christopher Perry Avatar answered Sep 19 '22 10:09

Christopher Perry