Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Cannot setClickable(false) a button after click

I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled (additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).

Here is a similar code:

Button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Button.setClickable(false);
        //do other things
    }
});

Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.

The problem is when I click the button very quickly, it seems the succeeding clicks are still handled. Is there a delay to the effects of setClickable()?

Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.

like image 203
martinC Avatar asked Jun 18 '26 06:06

martinC


1 Answers

Judging from your comment you probably need something like this.

  Boolean SWITCH_ON = false;

  Button.setOnClickListener(new OnClickListener() {
 
  public void onClick(View v) {
         if(!SWITCH_ON ){
                  SWITCH_ON = true;   
         }
    }
 });

Button.setOnLongClickListener(new View.OnLongClickListener() {

public boolean onLongClick(View v) {
    
   if(SWITCH_ON ){
      // do your task for long click here ...SWITCH_ON 
    }
   return true;
}
});
like image 164
Rohaitas Tanoli Avatar answered Jun 19 '26 19:06

Rohaitas Tanoli