4

The "solution #2 (dynamic)" in this question/answer post:

overlay two images in android to set an imageview

is very close to what I want to do, which is to dynamically create a layer-list (for a status bar notification icon, I want to build-up my icon in layers), but the icon assignment in the notification API requires a resource ID (which I want to call from a service).

I cannot figure-out how to build a dynamically build a layer-list without building hundreds of layer-list .xml files (for the various combinations of icons that I would like to be able to display). Daniel's "solution #1" works wonderfully for the static .xml files, but I'm looking for a more elegant, dynamic solution.

In the above post, 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);

appears to be what I want, but I don't know or understand how to "assign" the new layerDrawable to my notification icon (which takes a resource ID).

Thanks to all...stackoverflow is a wonderful resource!

Community
  • 1
  • 1
scary alien
  • 43
  • 1
  • 1
  • 6

2 Answers2

12

Use "getIdentifier" to get it.Suppose that I have a file bug.png in the "/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");      

reference:
anddev.org

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
3

There is no such thing as an ID for a Drawable created at runtime. Those IDs refer to int fields in the R class, automatically created from the xml files.

Since the LayerDrawable constructor requires just a Drawable array, you can provide those Drawables made from any method. An example, would be 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

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • Thanks, Aleadam (Adam?)! Sorry, I didn't see your response earlier ;). I'm guessing from your first sentence that I can only populate my status bar icon a statically-created resource :( I was hoping for a way to keep from having to build all of my icon combinations in layer-list .xml files. Cheers and thanks again for your reply. --Sean – scary alien Jun 10 '11 at 19:49
  • 1
    I believe that resources are the only way to reference notification icons. You should only need to add one icon per screen density, though. – Aleadam Jun 11 '11 at 04:10