2

I'm trying to make a pretty iPhone toggle button and stumbled upon this code from a generous StackOverflow user. So I basically cleaned up the example code until I'm left with what I need; one toggle button;

enter image description here

Everything works as expected, but the only thing that I don't know how to reverse the sides of the button. Currently, if the toggle is on the left side it means true, and the right side means false. I of course changed the wording, but that doesn't change the resulting boolean. I can of course also change my code to do the necessary stuff when it's false instead of true, but this would make our code totally unreadable.

I suppose I need to change something in this file, but in there I'm kinda lost. Would anybody know what I need to do to reverse the sides of the button?

===EDIT===

With the line toggleButton.setOnCheckedChangeListener(this); I set the listener to this method:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Log.d(TAG,"onChechedChanged(checked = "+isChecked+")");
    if (isChecked){
        doSomeStuffHere();
    }
    else {
        doNothingHere();
    }
}

Again, I can of course simply reverse the doSomeStuffHere() and doNothingHere() or even reverse the boolean before that if-statement, but that would make my code not very nice. I just want it to be correct from a coding perspective.

Community
  • 1
  • 1
kramer65
  • 50,427
  • 120
  • 308
  • 488
  • Why don't use just search for all references to the boolean property you want to work with and see where it's being set? – Simon Oct 03 '13 at 13:57
  • Ok, paste the code where you take advantage of those states. Can't you just `!` reverse the state in the vbariable you're keeping it ? – g00dy Oct 03 '13 at 14:06
  • @Simon - Thats unfortunately not so easy. I simply set a onCheckedChangeListener to onCheckedChanged(), which takes a buttonview and a boolean. Since I Override this method myself I don't know of any way in there to change the boolean. – kramer65 Oct 03 '13 at 14:07
  • @g00dy - Yes I can do that, but it simply looks like a mess. I'm getting true, reversing it to false, and then decide on the beasis of that. It would be more logical to change it somewhere when it is actually defined. – kramer65 Oct 03 '13 at 14:30
  • Then, you'll have to re-write probably the most part of the library from the link you provided above, or just that one function, where it is defined (this could lead to regressions). For this one particular button, isn't it for the best, if you just take the image resources from the github link and create (separately from the library) a custom toggle button, which to suit your needs? – g00dy Oct 03 '13 at 14:38

0 Answers0