Get linked image (Drawable) in ImageView android

i set the image to the image view control in android:

iv.setImageResource(R.drawable.image1); 

I want to get the Drawable associated with it, it looks like this:

 Drawable myDrawable = iv.getImageResource(); //wrong 

thanks!

+7
source share
5 answers

You can get pushed like

 Drawable myDrawable = iv.getDrawable(); 

You can compare it with a resource that you can use, for example

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

Unfortunately, there is no getImageResource() or getDrawableId() . But I created an easy workaround by using 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 want, 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 undoubtedly facilitated my work.

+7
source

Define Drawable first.

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

use Drawable drawable=imageview.getDrawable();

+5
source

This is a simple approach if you can use a member variable of a view identifier: just save the R.drawable identifier using v.setId (). Then return it using v.getId ().

0
source

All Articles