2

A drawable, which was created by XML, can be referenced in code by

    R.drawable.my_drawable

So, I assume that drawables do have a resource id.

When I look through the docs, i cant find a method:

    myDrawable.setId(id)

So, though I can construct a drawable in code, i dont see how I can assign a id to a drawable, which I created in code?

3
  • Why do you need to assign an id to the drawable? R.drawable.* is already a unique identifier to that resource.
    – Mark Pazon
    Apr 3, 2012 at 18:31
  • because I create a ShapeDrawable in code, so no xml file exists.
    – mrd
    Apr 3, 2012 at 18:57
  • Could you provide an example use case?
    – user340145
    Apr 3, 2012 at 19:31

1 Answer 1

-3

R.drawable.my_drawable is the unique id that android automatically generates for your drawable -- theres no need to set it, and if you need to reference it, the constant R.drawable.my_drawable works.

13
  • 1
    If I interpret mradlmaier's question correctly he wants to know how to set the ID of a drawable that he has generated in code, e.g. a BitmapDrawable.
    – user340145
    Apr 3, 2012 at 18:46
  • Then your solution is incorrect... BitmapDrawable doesn't extend View and doesn't have a setId method.
    – Benoir
    Apr 3, 2012 at 18:50
  • deleted my answer to @hanspeide, because he deleted his. :)
    – mrd
    Apr 3, 2012 at 19:02
  • You're right. Seems that I overlooked that mradlmaier was talking about a drawable. As that is the case I agree with your answer, and have deleted mine. Upvoted.
    – user340145
    Apr 3, 2012 at 19:02
  • this is exactly what I meant. Although, if created in by xml file, drawables have resource ids, there is no method to set the id of a drawable which was created by code, similiar to a View.
    – mrd
    Apr 3, 2012 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.