Android change the color of a button for 1second

Asked

Viewed 56 times

-1

I have an array that contains integer numbers from 0 to 4. {1,4,0,3,2} need a button to blink a certain color according to the sequence. How this is done ?

2 answers

1

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

UNTESTED and needs some adjustments, because some methods that seemed more obvious to me were not written, but the bulk of the logic is here.

Come on.

Create a pointer variable, qte_sequence and array to store 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;
            }
        }
    };  

Initialize the objects, assigning to each the click event to light when the player touches the button. I am using the Debouncedonclicklistener class to avoid double click, but you can use the one that suits you best.

 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);
                   }
                });

...

repeat for all others by changing what needs to be changed.

Creates 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’ll generate a 5-color sequence for the user to repeat

The randomly generated sequence was this:

1,2,2,4,3

Reset pointer variable and arrow qte_sequence = 5

I call the method AcendeCor( ponteiro )

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

  • Hello Reginaldo, sorry for the delay in answering you. I used your solution and it helped me to solve the problem! Thank you very much!

0

You can set the color you want and then use a handler to set 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);

Browser other questions tagged

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