Reputation: 99
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?
Upvotes: 0
Views: 272
Reputation: 5940
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);
Upvotes: 1