Skip to content
Advertisement

Android – programmatically change the state of a switch without triggering OnCheckChanged listener


I’m looking for a method of programmatically changing the state of an Android Switch widget using switch.setChecked(true); without triggering OnCheckedChangedlistener.

My first thought was to swap it out for an OnClickListener but as this only registers clicks and you are able to not only click but also slide a Switch then it’s not really fit for purpose as if the user was to slide the Switch from off to on then the Switch would actually do nothing as the user is not clicking…

If anyone’s got a solution or a smart work around for this, that would be awesome

Advertisement

Answer

Well, just before doing things in code with the switch you could just unregister the Listener, then do whatever you need to, and again register the listener.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement