2

So I know you can set the images of a ImageView like the following (assuming bun_picture is an ImageView):

bun_picture.setImageResource(R.drawable.rawbun);

but is there any way to check what image an ImageView is currently displaying? Something like (NOTE: .getImageResource doesn't exist, I'm just using it to get across what I want to do):

if(bun_picture.getImageResource() == R.drawable.rawbun) == true){
do something}

Obviously that doesn't work, but is there some equivalent I can use?

Thanks

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Evan
  • 1,892
  • 2
  • 19
  • 40
  • Possible duplicate of [How to check which current image resource is attached to ImageView in android xml?](https://stackoverflow.com/questions/23357706/how-to-check-which-current-image-resource-is-attached-to-imageview-in-android-xm) – Ali Ahsan Aug 14 '19 at 02:07

1 Answers1

1

Unless you have called mutate() on either of the drawables, you can check if two drawables represent the same image by comparing their ConstantState fields.

ConstantState mine = bun_picture.getConstantState();
ConstantState other = ContextCompat.getDrawable(context, R.drawable.rawbun).getConstantState();

if (mine.equals(other)) {
    // they are the same
}
Ben P.
  • 52,661
  • 6
  • 95
  • 123