Android change the color of a button for 1second

-1

I have a college job where I have to create a Genius game on Android. Where there are 4 buttons and for 1 second each button will blink by changing its background color so the user will have to perform the same color sequence by pressing the buttons in the same sequence as shown. My question would be how to change the background of the buttons for 1 second following the sequence. Can anyone help me?

    
asked by anonymous 26.11.2018 / 16:23

2 answers

1

I found it interesting and I decided to write the way it would solve this question.

  

NOT TESTED and lacks some tweaks, not least because some methods that I found to be most obvious were not written, but the bulk of the logic is here.

Come on.

Create a pointer variable, qte_sequence, and an array to store the sequences
  global.

Create a Handler that receives messages to change button colors:

private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    volta_a_cor_normal;
                    if ( ponteiro < qte_sequencia )  {
                        ponteiro++;
                        AcendeCor(ponteiro);
                    }
                    break;
                case 5:
                    seta_a_cor_vermelha;
                    break;
                case 10:
                    seta_a_cor_amarela;
                    break;
                case 15:
                    seta_a_cor_azul;
                    break;
                case 20:
                    seta_a_cor_verde;
                    break;
                default:
                    break;
            }
        }
    };  

Initializes the objects, assigning each the click event to light   when the player touches the button. I'm using the DebouncedOnClickListener class   to avoid double click, but you can use the one that suits you.

 btn_vermelho = (Button) findViewById(R.id.btnVermelho);
 btn.setOnClickListener(new DebouncedOnClickListener() {
            @Override
            public void onDebouncedClick(View v) throws UnsupportedEncodingException {
                    }
                     myHandler.sendEmptyMessage(5);
                     new Handler().postDelayed(new Runnable() {
                     @Override
                        public void run() {
                        myHandler.sendEmptyMessage(0);
                      } ,1000);
                   }
                });

...

Repeats for all others by changing what has to be changed.

Create the procedure that will turn on the buttons automatically.

private void AcendeCor(Integer ponteiro) {
       Integer botao = sequencia[ ponteiro ];
       switch (botão} {
            1 :  {
                   SetaBotaoAmarelo;
                   break;
                 }
            2 :  {
                   SetaBotaoVermelho;
                   break;
                 }                       
            3 :  {
                   SetaBotaoVerde;
                   break;
                 }      
            4 :  {
                   SetaBotaoAzul;
                   break;
                 }      
       }                 
       new Handler().postDelayed(new Runnable() {
                     @Override
                        public void run() {
                        myHandler.sendEmptyMessage(0);
                      } ,1000);
                   }

    }

Let's say I'm going to generate a 5-color sequence for the user to repeat

The randomly generated sequence was this:

1,2,2,4,3

Zero the variable pointer and arrow qte_sequencia = 5

I call the method AcendeCor( ponteiro )

The method receives the pointer, checks which button should light up accordingly     with the sequence. Lights up and initializes the Handler.     This in turn waits 1 second and calls myHandler which clears the button, increments the pointer, and repeats the process.

    
26.11.2018 / 18:02
0

You can set the color you want and then use handler to set to another color after 1 second:

Button btn_1 = findViewById(R.id.btn_1);

        btn_1.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

        new Handler().postDelayed(() ->
                btn_1.setBackgroundColor(getResources().getColor(R.color.colorAccent)), 1000);
    
26.11.2018 / 17:14