Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check which image is linked to ImageView in android?

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

like image 841
4this Avatar asked Dec 19 '22 22:12

4this


1 Answers

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");
}
like image 100
Mohsen Afshin Avatar answered Dec 22 '22 11:12

Mohsen Afshin