Jochen
Jochen

Reputation: 1

Find out which is the current background Drawable

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

Answers (2)

Ovidiu Latcu
Ovidiu Latcu

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

Reno
Reno

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:

  1. Bitmap: the simplest Drawable, a PNG or JPEG image.
  2. Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
  3. Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
  4. Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
  5. States: a compound drawable that selects one of a set of drawables based on its state.
  6. Levels: a compound drawable that selects one of a set of drawables based on its level.
  7. 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

Related Questions