title: Referencing a custom Shape class in XML
body: <p>I have been extending the standard range of <code>Shape</code> classes (<code>RectShape</code>, <code>OvalShape</code> and so on) by extending the <code>Shape</code> class to create my own custom set of shapes. For example, I have created a simple <code>TriangleShape</code> class, like so:</p>
  
  <pre><code>import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.Path;
  import android.graphics.drawable.shapes.Shape;
  
  public class TriangleLeftShape extends Shape {
  
  @Override
  public void draw(Canvas canvas, Paint paint) {
      Path path = new Path();
      path.setLastPoint(0, getHeight()/2);
      path.lineTo(getWidth(), getHeight());
      path.lineTo(getWidth(), 0);
      path.close();
      canvas.drawPath(path, paint);
      }
  }
  </code></pre>
  
  <p>What I would like to do is create a <code>Drawable</code> resource entirely in XML using this class. Is this possible?</p>
  
  <p>I'm aware that using one of the standard shapes is simply achieved by the following example, where the <code>&lt;shape&gt;</code> element represents a <code>ShapeDrawable</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" &gt;
  &lt;gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
          android:angle="270"/&gt;
  &lt;/shape&gt;
  </code></pre>
  
  <p>What I cannot see is how one would pass, in XML, a custom <code>Shape</code> class to this <code>ShapeDrawable</code> that's being defined in XML. I understand that the <code>android:shape</code> attribute is simply passing an enum value, which can only be rectangle, oval, line, or ring. It seems that there is no XML attribute to specify a custom <code>Shape</code> class. </p>
  
  <p>However, the <code>ShapeDrawable</code> has a <code>setShape()</code> method, which seems to suggest I could programmatically set my custom <code>Shape</code> class, but not do it via XML.</p>
  
  <p>How, if possible, can I make use of a custom <code>Shape</code> class in XML? I realise that I could create a custom <code>View</code> very easily to draw my basic shapes, but use of <code>Drawables</code> seems to have the advantage of being able to specify colours, etc. and other attributes in XML or styles / themes.</p>