How to change the color of the button that is pressed?

Asked

Viewed 775 times

0

I am developing an app that randomly generates an addition account and generates 4 values as possible answers and that are placed in 4 buttons (also in a random way).

I would like to know how I do, when I click on any of the buttons (the possible answer), only it change color and do not let select others (do not let others change color).

Note: This process of choice would be repeated 9 more times.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_realizar_contas);
        final TextView num1 = (TextView) findViewById(R.id.num1);
        final TextView num2 = (TextView) findViewById(R.id.num2);
        final Button op1 = (Button) findViewById(R.id.op1);
        final Button op2 = (Button) findViewById(R.id.op2);
        final Button op3 = (Button) findViewById(R.id.op3);
        final Button op4 = (Button) findViewById(R.id.op4);
        final Button avancar = (Button) findViewById(R.id.btn_avancar);

        esc = numeroDecisao();
        res1 = numeroAleatorio();
        res2 = numeroAleatorio();

        //Toast.makeText(getApplicationContext(), "Esc " + esc, Toast.LENGTH_SHORT).show();

        if (esc == 1) {
            val1 = res1 + res2;
            val2 = (numeroAleatorio() + numeroAleatorio()) + 1;
            val3 = (numeroAleatorio() + numeroAleatorio()) + 2;
            val4 = (numeroAleatorio() + numeroAleatorio()) + 3;

        } else if (esc == 2) {
            val1 = (numeroAleatorio() + numeroAleatorio()) + 3;
            val2 = res1 + res2;
            val3 = (numeroAleatorio() + numeroAleatorio()) + 7;
            val4 = (numeroAleatorio() + numeroAleatorio()) + 1;

        } else if (esc == 3) {
            val1 = (numeroAleatorio() + numeroAleatorio()) + 8;
            val2 = (numeroAleatorio() + numeroAleatorio()) + 9;
            val3 = res1 + res2;
            val4 = (numeroAleatorio() + numeroAleatorio()) + 10;
        } else if (esc == 4) {
            val1 = (numeroAleatorio() + numeroAleatorio()) + 7;
            val2 = (numeroAleatorio() + numeroAleatorio()) + 6;
            val3 = (numeroAleatorio() + numeroAleatorio()) + 5;
            val4 = res1 + res2;
        }

        num1.setText(Integer.toString(res1));
        num2.setText(Integer.toString(res2));
        op1.setText(Integer.toString(val1));
        op2.setText(Integer.toString(val2));
        op3.setText(Integer.toString(val3));
        op4.setText(Integer.toString(val4));

        op1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                op1.setBackgroundResource(R.drawable.botao_customizado);
                press = press + 1;
            }
        });


        //BOTÃO - AVANÇAR - Realizar a procdimento anterior 9 vezes
        avancar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cont = 0;
                //Toast.makeText(getApplicationContext(), "Acertos " + acertos, Toast.LENGTH_LONG).show();

                while (cont != 9) {
                    res1 = numeroAleatorio();
                    res2 = numeroAleatorio();
                    esc = numeroDecisao();

                    if (esc == 1) {
                        val1 = res1 + res2;
                        val2 = (numeroAleatorio() + numeroAleatorio()) + 9;
                        val3 = (numeroAleatorio() + numeroAleatorio()) + 3;
                        val4 = (numeroAleatorio() + numeroAleatorio()) + 1;

                    } else if (esc == 2) {
                        val1 = (numeroAleatorio() + numeroAleatorio()) + 4;
                        val2 = res1 + res2;
                        val3 = (numeroAleatorio() + numeroAleatorio()) + 5;
                        val4 = (numeroAleatorio() + numeroAleatorio()) + 7;

                    } else if (esc == 3) {
                        val1 = (numeroAleatorio() + numeroAleatorio()) + 8;
                        val2 = (numeroAleatorio() + numeroAleatorio()) + 9;
                        val3 = res1 + res2;
                        val4 = (numeroAleatorio() + numeroAleatorio()) + 10;
                    } else if (esc == 4) {
                        val1 = (numeroAleatorio() + numeroAleatorio()) + 10;
                        val2 = (numeroAleatorio() + numeroAleatorio()) + 3;
                        val3 = (numeroAleatorio() + numeroAleatorio()) + 1;
                        val4 = res1 + res2;
                    }

                    num1.setText(Integer.toString(res1));
                    num2.setText(Integer.toString(res2));
                    op1.setText(Integer.toString(val1));
                    op2.setText(Integer.toString(val2));
                    op3.setText(Integer.toString(val3));
                    op4.setText(Integer.toString(val4));
                    cont++;


                }
            }
        });
    }
  • 2

    What do you already have? Where exactly are you having problems? Is it with the color change? Is it with the deactivation of the other buttons? Is it with the algorithm? Break your problem in stages and try to solve one thing at a time. As it stands, the question is very wide.

  • Thanks for the tip, well let’s go for the first problem, I have an application that will repeat 10 times addition questions and that each time will generate 4 answers, I can change the color of the button the first time by clicking on the "answer", but already the second time onwards, the button gets the color of what I clicked earlier. How do I fix it?

  • You can paste here the code snippet where you change the button color to analyze?

  • I put, thanks!

1 answer

1

You can try using the function setBackgroundColor and insert a hexadecimal value of the colour. opt1.setBackgroundColor(0xFFFFA500);

If you already know CSS, you can change the color by changing the last 6 values (FFA500) by the color of choice. There are several hexadecimal color tables on the internet.

for more information see an English post on the subject. Android Button color Changing on onClick?

  • Thanks, I already got it here, I thought of a better way, without needing to change the color of the button. Thanks anyway!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.