Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 499
skyfoot Avatar asked Oct 30 '10 23:10

skyfoot


People also ask

What is stroke in android XML?

This is used to control the behavior of miter joins when the joins angle is sharp. This value must be >= 0.


1 Answers

Here's how I did a filled red circle with a one-pixel black border, with a white number 72 in the middle:

Create an XML file in res\drawable and name it appropriately, eg red_circle_black_border.xml

<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">     <item>         <shape android:shape="oval">             <solid android:color="@android:color/black" />         </shape>     </item>     <item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">         <shape android:shape="oval">             <solid android:color="@android:color/red" />         </shape>     </item> </layer-list> 

Then, to use it in your layout, declare it as follows:

<TextView     android:text="72"     android:textSize="14dp"     android:textStyle="bold"     android:background="@drawable/red_circle_black_border"      android:layout_width="22dp"     android:layout_height="22dp"     android:gravity="center_vertical|center_horizontal"     android:textColor="@android:color/white" /> 

Obviously, change the textSize and layout_width/height as required :-)

like image 171
Kenton Price Avatar answered Sep 20 '22 14:09

Kenton Price