2

I want to use a number of ShapeDrawables in my application, which are all similar, but with different colours etc. Obviously I could just define them all in separate xml files, but is there a more efficient way to have one xml definition, instantiate various objects and change the colour either in code or xml? You could perhaps do this by calling mutate() on one ShapeDrawable defined in xml, but this returns a Drawable, rather than a shape drawable.

Stev_k
  • 2,118
  • 3
  • 22
  • 36

2 Answers2

0

Actually it is possible to do what you asked for,
this post shows you how to do it.

I tried it and it works perfectly. I haven't found though how to refer to a single drawable from the list.
It seems Level lists are meant to be used into a single object to represent different states of the object. Alhtough in this case we are using it as a drawable container to avoid having many small files.
Maybe future anrdroid releases will support a dedicated drawable container.

Community
  • 1
  • 1
ilomambo
  • 8,290
  • 12
  • 57
  • 106
0

Maybe use GradientDrawable instead of ShapeDrawable.

Jett Hsieh
  • 3,159
  • 27
  • 33
  • As far as I know (unless I'm missing something in the docs) that just enables you to add a colour gradient to your shapes - not quite what I was after. – Stev_k Aug 17 '10 at 13:59
  • looks like one way to do it is using the setColorFilter(color, PorterDuff.Mode.Multiply) method on a single drawable defined in xml and inflated at runtime. For more info see http://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color – Stev_k Sep 04 '10 at 22:31