RainbowJeremy
RainbowJeremy

Reputation: 73

Buttons not responding in Android

I have buttons in my application that have numbers as their text. When the buttons are pressed in a certain order, the numbers on the buttons change and the buttons should be able to be pressed again and have the cycle continue with the numbers changing. This works the first time, but when the numbers change, them being pressed does not seem to do anything.

public class MainActivity extends Activity {

int b1 = (int) (Math.random() * 100.0);
int b2 = (int) (Math.random() * 100.0);
int b3 = (int) (Math.random() * 100.0);
int b4 = (int) (Math.random() * 100.0);
int last = 0;
int[] nums = { b1, b2, b3, b4 };
int i = 0;
String res = "";

Button button1, button2, button3, button4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Arrays.sort(nums);

    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2); 
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);

    button1.setText(Integer.toString(b1));
    button2.setText(Integer.toString(b2));
    button3.setText(Integer.toString(b3));
    button4.setText(Integer.toString(b4));

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b1;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b2;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b3;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int value = b4;
            checkOrder(value);
            TextView result = (TextView) findViewById(R.id.textView1);
            result.setText(res);

        }
    });

}

private void checkOrder(int value) {
    if (value == nums[i]) {
        if (i == 3) {
            button1.setBackgroundResource(android.R.drawable.btn_default);
            button2.setBackgroundResource(android.R.drawable.btn_default);
            button3.setBackgroundResource(android.R.drawable.btn_default);
            button4.setBackgroundResource(android.R.drawable.btn_default);
            b1 = (int) (Math.random() * 100.0);
            b2 = (int) (Math.random() * 100.0);
            b3 = (int) (Math.random() * 100.0);
            b4 = (int) (Math.random() * 100.0);
            button1.setText(Integer.toString(b1));
            button2.setText(Integer.toString(b2));
            button3.setText(Integer.toString(b3));
            button4.setText(Integer.toString(b4));
            i = 0;

        } else {
            if (value == b1) {
                button1.setBackgroundColor(Color.GREEN);
            } else if (value == b2) {
                button2.setBackgroundColor(Color.GREEN);
            } else if (value == b3) {
                button3.setBackgroundColor(Color.GREEN);
            } else if (value == b4) {
                button4.setBackgroundColor(Color.GREEN);
            }
            i++;
        }
    } else {

    }
}

I think this has something do with the fact that the onCreate method only runs at the start of the application. How can I keep having my buttons' presses be responsive?

Upvotes: 0

Views: 80

Answers (1)

codePG
codePG

Reputation: 1744

May be you will have to reassign the values for

int[] nums = { b1, b2, b3, b4 };

after generating new values for b1, b2, b3, b4 in the checkOrder method.

As when you try to equate if (value == nums[i]) for the second time, the condition is always false. The values in nums are the previously generated values.

I hope that helps!

Upvotes: 3

Related Questions