0

I want to clear the present background image before I show the new one. I have tried setBackgroundResource(0) and setBackgroundColor(Color.TRANSPARENT) as well but it doesn't work.

Java:

public void decideclick(){
    decidebutton = (Button) findViewById(R.id.decideimg);
    resultview = (ImageView) findViewById(R.id.imageView);
    resultview2 = (ImageView) findViewById(R.id.imageView2);

    decidebutton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            resultview.setBackgroundResource(0);
            resultview2.setBackgroundColor(Color.TRANSPARENT);
            SystemClock.sleep(2000);

            if(player_choose == 5)
                resultview.setBackgroundResource(R.drawable.abc);
            else if(player_choose == 2)
                resultview.setBackgroundResource(R.drawable.edf);
            else if(player_choose == 0)
                resultview.setBackgroundResource(R.drawable.ghi);

        }
    });

}

The result is that the present imageView and imageView2 do not disappear and the new images come out after 2 seconds. Why doesn't setBackgroundResource(0) work?

Keroro Chan
  • 99
  • 1
  • 10
  • Can use this? `view.setVisibility(View.GONE);` and `view.setVisibility(View.INVISIBLE); ` – chohyunwook Jul 20 '16 at 02:56
  • 3
    Don't sleep the main thread – tachyonflux Jul 20 '16 at 02:56
  • tachyonflux, I don't get it. Why shouldn't I sleep the thread? – Keroro Chan Jul 20 '16 at 03:07
  • I change resultview.setBackgroundResource(0); into resultview.setBackgroundResource(R.drawable.testing); the testing.jpg doesn't come out. So, I think the problem comes from SystemClock.sleep(2000); but Why shouldn't I sleep the thread? And how can I clear the present image before the new one comes out after 2 seconds? – Keroro Chan Jul 20 '16 at 03:20

1 Answers1

1

It's most likely because you are sleeping the thread before it has a chance to redraw that view. You won't see the changes until this particular message has finished being handled which then gives the UI a chance to redraw.

Try this:

resultview.setBackgroundResource(0);

new Handler().postDelayed(new Runnable() {
    public void run() {
        if(player_choose == 5)
            resultview.setBackgroundResource(R.drawable.abc);
        else if(player_choose == 2)
            resultview.setBackgroundResource(R.drawable.edf);
        else if(player_choose == 0)
            resultview.setBackgroundResource(R.drawable.ghi);
    }
}, 2000);
Ross Hambrick
  • 5,880
  • 2
  • 43
  • 34