Home » Android » android – How to get Image Resource

android – How to get Image Resource

Posted by: admin May 14, 2020 Leave a comment

Questions:

How should I get current image resource of an imageButton?
I need something like this:

imageButton.getImageResource(); 

Thanks a lot

How to&Answers:

I think you can’t, current API doesn’t allow this.
But if you really need this, you can do something like this:

imageButton.setImageResource(R.drawable.your_resource);
imageButton.setTag(R.drawable.your_resource);
//
// somewhere later...
//
Integer resource = (Integer)imageButton.getTag();

Answer:

You can use imageButton.getDrawable() to get the drawable of the ImageButton object and then use setImageDrawable() instead of setImageResource().

Answer:

Use this

imageButton.setImageResource(R.drawable.your_resource);
imageButton.setTag(R.drawable.your_resource);

Integer resource = (Integer) imageButton.getTag();

if(resource == R.drawable.your_resource){
    // do something
}else {
}