user3503550
user3503550

Reputation: 83

Android change edittext background

I have an EditText (et) and a button (bt). When I press bt, et should change the background color to green, and after 1 second return white. My code is something like this:

    bt.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View view){
            answer.setBackgroundColor(Color.parseColor("#00FF00"));
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){}
            answer.setBackgroundColor(Color.parseColor("#FFFFFF"));
        }
    });

The problem that I noticed is that the color of et changes only when the code is completely executed, not when the method is called! In other words, et is always white, because the change to green never happens. Is there an alternative to that code? Thank you very much in advance.

Upvotes: 0

Views: 131

Answers (1)

Akash Moradiya
Akash Moradiya

Reputation: 3322

try this,

 bt.setOnClickListener(new Button.OnClickListener(){
                                    @Override
                                    public void onClick(View view){
                                        answer.setBackgroundColor(Color.parseColor("#00FF00"));
                                        new Handler().postDelayed(new Runnable() {

                                            @Override
                                            public void run() {
                                                // TODO Auto-generated method stub
                                                    answer.setBackgroundColor(Color.parseColor("#FFFFFF"));
                                            }
                                        }, 1000);
                                    }
                                });

Upvotes: 2

Related Questions