1

I encounter the problem with Switch in android. Here's my code to handle OnCheckedChangeListener of Switch:

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
    if (compoundButton.isPressed()) {
        myCodeHere();
    }
}

The condition if (compoundButton.isPressed()) to avoid onCheckedChanged called automatically. But when I slider Switch then myCodeHere() don't trigger.

Please give me some hint to resolve this issue. Or any way to disable slider of Switch ? Thanks a lot

1 Answers1

0
onCheckedChanged(CompoundButton buttonView, boolean isChecked)

Called when the checked state of a compound button has changed. You can use the boolean isChecked to perform actions whenever the switch is turned ON/OFF

if (isChecked) {
// switch is ON perform some action
} else {
// switch is OFF perform some different action
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
kevz
  • 2,727
  • 14
  • 39
  • If don't use condition, I will encounter another problem same at here : http://stackoverflow.com/questions/27139262/change-switch-state-without-animation – Khoa Nguyễn Feb 22 '16 at 06:37
  • @KhoaNguyễn: So the problem is animation redrawn, Did u tried the suggested solutions for that problem? – kevz Feb 22 '16 at 06:44
  • I tried the way of Mr.alex, but it don't work. Do you have any idea for this issue ? – Khoa Nguyễn Feb 22 '16 at 06:51
  • @KhoaNguyễn: Well u can try all the suggested answers. Sorry I don't any ideas over it – kevz Feb 22 '16 at 06:52