How to get the Android resource identifier from a dynamically created (in Java) list layer / LayerDrawable?

"Decision No. 2 (dynamic)" in this question / answer:

overlay two images in android to set image

very close to what I want to do, namely, dynamically create a list of layers (for the notification icon in the status bar, I want to create my own icon in the layers), but the purpose of the icons in the notification API requires the resource identifier (which I want to call from the service) .

I cannot figure out how to create a dynamic assembly of a list of layers without creating hundreds of XML files with a single layer (for various icon combinations that I would like to display). Daniel "Solution # 1" works great for static .xml files, but I'm looking for a more elegant dynamic solution.

In the above message, the code snippet:

Resources r = getResources(); Drawable[] layers = new Drawable[2]; layers[0] = r.getDrawable(R.drawable.t); layers[1] = r.getDrawable(R.drawable.tt); LayerDrawable layerDrawable = new LayerDrawable(layers); 

it looks like what I want, but I don’t know and don’t understand how to β€œassign” a new layerDrawable to my notification icon (which accepts the resource identifier).

Thanks to everyone ... stackoverflow is a great resource!

+4
source share
2 answers

There is no such thing as an ID for Drawable created at runtime. These identifiers refer to class R int fields automatically generated from xml files.

Since the LayerDrawable constructor only requires a Drawable array, you can provide these Drawables made from any method. An example is the static method Drawable.createFromStream(InputStream is, String srcName) .

http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromStream%28java.io.InputStream,%20java.lang.String%29

+3
source

Use " getIdentifier " to get it. Suppose I have a bug.png file in the file "/ res / drawable /", so I get its ResourceID with the following code:

 int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null); 

//or

 int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject"); 

link:
anddev.org

+11
source

All Articles