Krzysztof Kozmic Krzysztof Kozmic - 1 year ago 114
Android Question

Creating ring shape in Android code

I have the following shape XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:a=""

<!-- some other stuff goes here -->


I would like to use code instead to create this shape, since some things need to be calculated on the fly before I do it, so static pre-defined layout doesn't cut it.

I'm new to Android and can't quite figure out how XML translates to code, and there's no RingShape class inheriting from Shape.

In addition to answering just this question, if there's a guide somewhere that details relation between XML and Java code and how XML gets processed in order to end up on the screen I would appreciate a link too. Thanks.

Answer Source

Ring and other shapes are GradientDrawables.

If you look at the source code for GradientDrawable, you'll see it looks like certain properties (like innerRadius) can only be defined through XML... they are not exposed through accessor methods. The relevant state is also unhelpfully private to the class, so subclassing is no help either.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download