16

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

imageButton.getImageResource(); 

Thanks a lot

MOHRE
  • 1,096
  • 4
  • 15
  • 28

3 Answers3

33

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();
dimsuz
  • 8,969
  • 8
  • 54
  • 88
3

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

Alexander Rossa
  • 1,900
  • 1
  • 22
  • 37
-1

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 {
}
Mohammad nabil
  • 1,010
  • 2
  • 12
  • 23