Get associated image (Drawable) in ImageView android

You can get the drawable like

Drawable myDrawable = iv.getDrawable();

You can compare it with a drawable resource like

if(iv.getDrawable()==getResources().getDrawable(R.drawable.image1)){
    //do work here
}

Define the Drawable first.

Drawable myDrawable = getResources().getDrawable(R.drawable.image1);
iv.setImageDrawable(myDrawable);

Unfortunately, there is no getImageResource() or getDrawableId(). But, I created a simple workaround by using the ImageView tags.

In onCreate():

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);

imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);

Then, if you like, you can create a simple function to get the drawable id:

private int getImageResource(ImageView iv) {
    return (Integer) iv.getTag();
}

I hope this helps you, it sure made my work easier.


use Drawable drawable=imageview.getDrawable();