Pushan Gupta
Pushan Gupta

Reputation: 3825

How to check if a particular image exists inside an ImageView

I want to know that if a particular image rest inside an imageview.

Something like this

if(imagevew.getImage()==R.drawable.image1){
//do some stuff
}

Upvotes: 1

Views: 938

Answers (3)

Avijit Karmakar
Avijit Karmakar

Reputation: 9388

getConstantState() method of drawable return a Drawable.ConstantState instance that holds the shared state of this drawable.

if (imageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.image1).getConstantState()) {
      Toast.makeText(_con, "Image is image1", Toast.LENGTH_LONG).show();
} else {
     Toast.makeText(_con, "Image isn't image1", Toast.LENGTH_LONG).show(); \
}

Upvotes: 3

yanivtwin
yanivtwin

Reputation: 625

There is no straight forward answer here , you can do something like settag when doing the setresource that way you'll know the name of the resource ,

something like :

imageView.setTag(R.drawable.drawablename);
imageView.setImageResource(R.drawable.drawablename);
String backgroundImageName = String.valueOf(imageView.getTag());

and than do a check like :

if (backgroundImageName.equals("BG"))  // "BG" is the tag that you set previously
    {
      Toast.makeText(_con, "Image is ivPic", Toast.LENGTH_LONG).show(); 
    } 
  else 
    {
     Toast.makeText(_con, "Image isn't ivPic", Toast.LENGTH_LONG).show(); 
    }

Upvotes: 0

Stanojkovic
Stanojkovic

Reputation: 1632

You could try like this:

ImageView v = (ImageView)findViewbyId(R.id.img);
String backgroundImageName = String.valueOf(v.getTag());    

if( backgroundImageName.equals(ImageName))
{
   //do something
} 
else{
   //do something
}

Upvotes: 1

Related Questions