Well i have one button and one ImageView in my app. What i am trying to do is when i pressing on the button then the image at the ImageView will change. All i have are two pics file.
What i am trying to do is - if the first pic is linked to the ImageView than change it to pic2 by clicking on the button, and if pic2 is linked than a click on the button will change it back to the first pic file.
here's the onClick method i tried to use:
public void onClick(View v) {
ImageView ib1 = (ImageView)findViewById(R.id.imageView1)
View p1 = findViewById(R.drawable.pic1);
if(ib1.getResources()==R.drawable.pic1){
ib1.setImageResource(R.drawable.pic2);
}else{
ib1.setImageResource(R.drawable.pic1);
}
}
Thanks for any kind of help
Rather than checking the image, I would suggest set the information tag of the ImageView
each time you change the image, like:
if(ib1.getTag() != null && ib1.getTag().toString().equals("pic1")){
ib1.setImageResource(R.drawable.pic2);
ib1.setTag("pic2");
} else {
ib1.setImageResource(R.drawable.pic1);
ib1.setTag("pic1");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With