I have one ImageView
and set a drawable on it. Now I need to get the ID of the drawable on click event of ImageView
dynamically. How can I get it?
imgtopcolor = (ImageView) findViewById(R.id.topcolor); imgtopcolor.setImageResource(R.drawable.dr); // How do I get this back?
Now on touch event of imgtopcolor
i want to need drawable id because I am setting different drawable each time and want to compare the drawable with other
drawable id in the view's id: use v. setId() . Then get it back with v. getId() .
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling. To learn more about Drawables, see: Drawable Resources.
I think if I understand correctly this is what you are doing.
ImageView view = (ImageView) findViewById(R.id.someImage); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { ImageView imageView = (ImageView) view; assert(R.id.someImage == imageView.getId()); switch(getDrawableId(imageView)) { case R.drawable.foo: imageView.setDrawableResource(R.drawable.bar); break; case R.drawable.bar: default: imageView.setDrawableResource(R.drawable.foo); break; } });
Right? So that function getDrawableId()
doesn't exist. You can't get a the id that a drawable was instantiated from because the id is just a reference to the location of data on the device on how to construct a drawable. Once the drawable is constructed it doesn't have a way to get back the resourceId that was used to create it. But you could make it work something like this using tags
ImageView view = (ImageView) findViewById(R.id.someImage); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { ImageView imageView = (ImageView) view; assert(R.id.someImage == imageView.getId()); // See here Integer integer = (Integer) imageView.getTag(); integer = integer == null ? 0 : integer; switch(integer) { case R.drawable.foo: imageView.setDrawableResource(R.drawable.bar); imageView.setTag(R.drawable.bar); break; case R.drawable.bar: default: imageView.setDrawableResource(R.drawable.foo); imageView.setTag(R.drawable.foo); break; } });
I answered something like this in another question already, but will change it just a little for this one.
Unfortunately, there is no getImageResource()
or getDrawableId()
. But, I created a simple workaround by using the 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 like, you can create a simple function to get the drawable id:
private int getDrawableId(ImageView iv) { return (Integer) iv.getTag(); }
Too easy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With