Search code examples
androidxmldrawableshapes

Drawing multiple shapes with ShapeDrawable in xml with Android


I am currently drawing a number of circles on a canvas in a custom view in code. the circles are static and do not change. I would like to draw them using a ShapeDrawable in xml to help clean up my code. I will have a number of different drawables which the user can select and therefore I don't want to do this in code. having 3 or 4 xml drawables seems a lot neater to me.

I have created one circle in xml using a ShapeDrawable but am unable to add more than one shape to the xml.

How do I add multiple shapes to an xml document using ShapeDrawable.


Solution

  • I think I have found a solution to add multiple shapes to my xml file using Layer-list. I havent tried this yet but this seems t be exactly what I want as the shapes will be draw in the order of their array index.

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
       <item>
          <shape>
             <solid android:color="#FFF8F8F8" />
          </shape>
       </item>
       <item android:top="23px">
          <shape>
             <solid android:color="#FFE7E7E8" />
          </shape>
       </item>
    </layer-list>
    

    Blog post

    Android docs