0

I basically got hits:

int zander = 1;
if(zander == 1) {
    button01.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        public void onClick(View v) {
            ((Button) android.findViewById(R.id.one)).setBackground(getResources().getDrawable(R.drawable.border_inner_green));
            int zander =+ 1;
            System.out.println(zander+"green");

        }
    });
} else if(zander == 2){
    button01.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        public void onClick(View v) {
            ((Button) android.findViewById(R.id.two)).setBackground(getResources().getDrawable(R.drawable.border_inner_red));
            int zander =- 1;
            System.out.println(zander+"red");
        }
    });
}

So when I click at the button, the background color changes to green in drawable.border_inner_green. I tried to increase an integer value just to get so to say the second click of the user to make the background red afterwards. But somehow this won't work I keep getting green. Does anyone have one idea?

3 Answers3

0
int zander = 1;
button01.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        public void onClick(View v) {
            if (zander == 1) {
                 ((Button) android.findViewById(R.id.one)).setBackground(getResources().getDrawable(R.drawable.border_inner_green));
                 zander = 2;
                 System.out.println(zander+"green");
            } else {

                 ((Button) android.findViewById(R.id.two)).setBackground(getResources().getDrawable(R.drawable.border_inner_red));
                zander = 1;
                System.out.println(zander+"red");
            }
    });

You need to set the onclicklistener once, and put your logic in there

RuAware
  • 979
  • 1
  • 9
  • 26
  • ok thanks but I get an error inside the method "if(zander = 1)" variable is accessed from within inner class needs to be declared final and should it not be zander == 1? – asdasdasda Aug 05 '15 at 19:41
  • add int zander as a class variable – RuAware Aug 05 '15 at 19:46
0

You have multiple declarations of the int zander variable - each declaration creates a separate value. You should only create one onClickListener handler too. Try moving the variable outside of the method and make it a field in the class:

class SomeClass {
  private int zander = 1;

  void onCreate(Bundle savedInstanceState) {

        button01.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            public void onClick(View v) {
                int color;
                if (zander == 1) {
                    color = R.drawable.border_inner_red;
                    zander++;
                } else {
                    color = R.drawable.border_inner_green;
                    zander--;
                }
                ((Button) android.findViewById(R.id.one)).setBackground(getResources().getDrawable(color));
            }
        });

}
adelphus
  • 10,116
  • 5
  • 36
  • 46
0

you have 2 instance of zander, one is declared outside the button click and another inside it.

When you click the button all you are doing is changing the inner zander and not the outer.

basically this

public void onClick(View v) {
        ((Button) android.findViewById(R.id.one)).setBackground(getResources().getDrawable(R.drawable.border_inner_green));
        int zander =+ 1;
        System.out.println(zander+"green");

    }

needs to look like this

public void onClick(View v) {
        ((Button) android.findViewById(R.id.one)).setBackground(getResources().getDrawable(R.drawable.border_inner_green));
        zander++;
        System.out.println(zander+"green");

    }

notice I removed the int fron the zander in the onClick. also if you are incrementing all you should do it zander++. Same goes for the other onClick method you have

tyczj
  • 71,600
  • 54
  • 194
  • 296