0

Basically what I'm trying to do is setting the color of the buttons. The "ganzjahrbutton" includes the other buttons. Therefore I wanna change the color of all the buttons (besides the ganzjahrbutton) to the grey color (162,162,162) if it's clicked. If any of the other buttons is clicked, they should turn green and the ganzjahrbutton should become grey again. It kinda works, but the buttons need to be pressed twice. Does anybody have an idea why?

        switch (v.getId()){

            case R.id.ganzjahrbutton:
                ganzjahrbtnstate = !ganzjahrbtnstate;
                if (ganzjahrbtnstate==true){
                    ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
                    fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
                    sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
                    herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
                    winterbutton.setBackgroundColor(Color.rgb(162,162,162));
                }
                else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
                break;

            case R.id.fruhlingbutton:
                fruhlingbtnstate = !fruhlingbtnstate;
                if (fruhlingbtnstate==true){fruhlungbutton.setBackgroundColor(Color.rgb(30,168,1));
                    ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
                else {fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));}
                break;



            case R.id.sommerbutton:
                sommerbtnstate = !sommerbtnstate;
                if (sommerbtnstate==true){sommerbutton.setBackgroundColor(Color.rgb(30,168,1));
                    ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
                else {sommerbutton.setBackgroundColor(Color.rgb(162,162,162));}
                break;



            case R.id.herbstbutton:
                herbstbtnstate = !herbstbtnstate;
                if (herbstbtnstate==true){herbstbutton.setBackgroundColor(Color.rgb(30,168,1));
                    ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
                else {herbstbutton.setBackgroundColor(Color.rgb(162,162,161));}
                break;



            case R.id.winterbutton:
                winterbtnstate = !winterbtnstate;
                if (winterbtnstate==true){winterbutton.setBackgroundColor(Color.rgb(30,168,1));
                    ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162)); }
                else {winterbutton.setBackgroundColor(Color.rgb(162,162,162));}
                break;
  • This might helps you instead of back button try with your own buttons. https://stackoverflow.com/questions/8430805/clicking-the-back-button-twice-to-exit-an-activity – TRK P Sep 04 '20 at 11:40
  • 1
    I'm pretty sure it has to do with inverting the booleans (`ganzjahrbtnstate = !ganzjahrbtnstate;`) then having an if statement. The first time you click the button, the bool is set to false and if condition is false, second time the bool is set to true and condition is true. Please do code in English instead of your native language so it's easier to understand the code – Alex Mandelias Sep 04 '20 at 11:44
  • Why don't you debug why this happens and find out? Your snippet doesn't have enough code for us to debug your code for you. – M. Prokhorov Sep 04 '20 at 12:47

1 Answers1

0

Move the lines where you change the boolean states to the end of cases just before break statements

         case R.id.ganzjahrbutton:
            ****FROM HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
            if (ganzjahrbtnstate==true){
                ganzjahrbutton.setBackgroundColor(Color.rgb(30,168,1));
                fruhlungbutton.setBackgroundColor(Color.rgb(162,162,162));
                sommerbutton.setBackgroundColor(Color.rgb(162,162,162));
                herbstbutton.setBackgroundColor(Color.rgb(162,162,162));
                winterbutton.setBackgroundColor(Color.rgb(162,162,162));
            }
            else {ganzjahrbutton.setBackgroundColor(Color.rgb(162,162,162));}
           TO HERE*****ganzjahrbtnstate = !ganzjahrbtnstate;****
            break;
  • Well, but in the beginning, while declaring the variables, I declared the booleans as false since I don't want the buttons to be green. Therefore moving that line to the end wouldn't trigger the if statement, would it? –  Sep 04 '20 at 14:31