My question is how do you check the image’s resource? In my onClick method there’s a countdown (which I summarized to avoid long post) which on every tick randomizes a boolean. If true, img1 resource is set to headAppear.jpg. If false, img1 sets to headHide.jpg.
What code will I put in the onClick() to check whether the picture is headAppear or headHide? What I want to do is, if I clicked img1 and it shows headAppear, score++. Else, if it shows headHide, score--.
In my onCreate() method:
ImageView img1 = (ImageView) findViewById(R.id.pic1);
img1.setOnClickListener( this );
//Countdown method here
var = aRandom.nextBoolean();
if( var == true ) {
img1.setImageResource( R.drawable.headAppear );
} else {
img1.setImageResource( R.drawable.headHide );
}
//End of countdown method
In my onClick(View v) method:
if (v == img1) {
//insert code here
}
You can not restore the image resource from the contains of an
ImageViewI will recommend you to have separate flag that you set in your if /else and us it to determine what is being shown. That way your code will become:And the check:
Or you can even make your
varvariable with class visibility.