Home > Software engineering >  How to check if an ImageView contains a specific drawable?
How to check if an ImageView contains a specific drawable?

Time:01-18

The first answer posted in this link:

Get associated image (Drawable) in ImageView android

is incorrect and doesn't work.

I logged the two strings out:

android.graphics.drawable.BitmapDrawable$BitmapState@d336646
android.graphics.drawable.BitmapDrawable$BitmapState@dc32534

...and they are different

I also tried something like this:

(garbage.getDrawable().getConstantState() !=
                        ContextCompat.getDrawable(getApplicationContext(), R.drawable.next_trash).getConstantState())

but it doesn't work either so I'm unsure of what to do.

Are there any other ways to check if an ImageView contains a specific drawable?

Thanks!

CodePudding user response:

you can try tag of Imageview.

    Log.d("zzzzz", "${R.drawable.ic_ic24_outline_cart_clear}")
    binding.image.setImageResource(R.drawable.ic_ic24_outline_cart_clear)
    binding.image.tag = R.drawable.ic_ic24_outline_cart_clear
    Log.d("zzzzz", "${binding.image.tag}")
    Log.d("zzzz", "${binding.image.tag == R.drawable.ic_ic24_outline_cart_clear}")

CodePudding user response:

What you need is a structure where you have a class of lets say Drawables in the format

data class Drawables(var drawableImage:Int, var drawableTag:String)

Then create objects of type Drawables and set the tag and photo from the data itself. That way you can check which drawable was set.

Stackoverflow answer on imageview tags to check content

CodePudding user response:

I changed the line to this instead of using ContextCompat:

garbage.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.next_trash).getConstantState()

...and was able to compare successfully.

  •  Tags:  
  • Related