Reputation: 1
inside my code, a View gets a new background:
myView.setBackgroundResource(R.drawable.foo);
OK, it mustn't be "foo", there are a dozen different Drawables possible. That's causing my problem: Later I need to know, which of these Drawables is shown actually. But I'm not able to find out the resId neither the name of the background, and also I didn't find a way to compare these drawables:
Drawable myDraw = myView.getBackground();
Drawable compareDraw = getResources().getDrawable(R.drawable.foo);
They aren't equal. Hm. How can I find the current backgrund image?
Thanks in advance
TIA Jo
Upvotes: 0
Views: 1212
Reputation: 72321
If you set your background why don't you keep a class member
int myBackground
that represents your currently background resource identifier. And each time you make
myView.setBackgroundResource(R.id.myBack);
you also set :
myBackground=R.id.myBack;
and in this way you always know the ID of your background resource.
Upvotes: 2
Reputation: 33792
Drawable is an abstract class, it can take many forms. From the documentation:
Though usually not visible to the application, Drawables may take a variety of forms:
- Bitmap: the simplest Drawable, a PNG or JPEG image.
- Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
- Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
- Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
- States: a compound drawable that selects one of a set of drawables based on its state.
- Levels: a compound drawable that selects one of a set of drawables based on its level.
- Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.
Internally I guess the bitmap may be the same, but the Drawable are of a different kind.
Upvotes: 0