i have switch widget per xml below , have ontoggleclicked function working fine when click on switch. noticed can slide switch 1 position , onclick event not triggered in case. how disable sliding of switch or how monitor state change of switch?? lot.
there 1 way have found listen sliding event of switch, using oncheckedchangelistener class compoundbutton. this, example:
exampleswitch = (switch) findviewbyid(r.id.exampleswitchid); exampleswitch.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { //your actions when button has changed status //the boolean ischecked tells if button activated or not } };);
the problem might have is, if change state of button programatically (exampleswitch.setchecked(boolean)) fire listener. if don't want happen, before setting state of button set listener null. example:
exampleswitch.setoncheckedchangelistener(null); exampleswitch.setchecked(boolean); exampleswitch.setoncheckedchangelistener(listener);
i hope help.