3

i want to check if a Imageview has a specific R.drawable and if so change it with another image. But it does not work this way.

xml code

 <ImageView
        android:id="@+id/Picture"
        android:src="@drawable/apicture"
 </ImageView>

java code

Image = (ImageView) findViewById(R.id.Picture);

public void coolMethod()
{
    if(Image.equals(R.drawable.apicture){

        Image.setImageResource(R.drawable.anotherpicture);
     }
}
wick.ed
  • 473
  • 7
  • 15
Nilsono
  • 43
  • 1
  • 4

1 Answers1

3

There can be multiple approaches to achieve this

1.Set drawable id as tag in image view before setting the drawable and compare this tag to determine if it is the same image.

image.setTag(R.drawable.apicture);

to check

if ((int) image.getTag() == R.drawable.apicture) {
      // do your stuff
 }

2.You need to match drawables if they are same

Drawable drawable=image.getDrawable();
Drawable drawable1=context.getDrawable(R.drawable.apicture);
if(drawable.equals(drawable1)){
image.setImageResource(R.drawable.anotherpicture);
}
Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49