user2863638
user2863638

Reputation: 121

How to disable multiple clicks on button in Android

When you click button in my app if you are fast enough before the screen/popup loads it loads them multiple times. I know how to disable the click on the button but that's not an option, because when you close the popup or return to the previous screen the button is disabled. I tried with Handler and Runnable to wait for 1s before the button is active again but this solution is not optimal in case if the OS needs more time to open the next screen. So I am searching for the most optimal solution. Any ideas?

Edit: setClickable(false) and then setting it back to true doesn't work because it loads my screen/popup slower than expected the button is enabled again and it opens the screen/popup multiple times again.

Upvotes: 1

Views: 1569

Answers (5)

Ramesh
Ramesh

Reputation: 54

You can disable the multiple click at the same time using the following code

private boolean isClicked;

@Override
public void onClick(final View v) {

    if(isClicked) {
        return;
    }
    isClicked = true;
    v.postDelayed(new Runnable() {

        @Override
        public void run() {
            isClicked = false;
        }
    }, 1000);
}

Upvotes: 2

Ahmad Raza
Ahmad Raza

Reputation: 2850

You can stop multiple operations by this way.

button.setOnClickListener(new OnClickListener(){
@Override
public void onClick()
{
    performOperation();

}

});

public void performOperation()
{
static boolean working = true;
if(working)
{
return;
}
working = true;

//Do you work here;


working = false;
}

Upvotes: -1

Priyanka
Priyanka

Reputation: 675

Maintain one variable on button onClick listener and change the value to determine when you want to click button..

Upvotes: 0

wvdz
wvdz

Reputation: 16641

Implement logic in your onClick to determine whether you want to ignore the click.

Upvotes: 1

Yauraw Gadav
Yauraw Gadav

Reputation: 1746

You can disable the button. When you close the popup enable the button and when the popup is visible make it disable. Keep listening the actions for popup and when the user get back to the previous screen.

Upvotes: 0

Related Questions