title: Android - how to define ShapeDrawables programmatically?
body: <p>What I'm trying to achieve is to use a Drawable with a couple of layers inside it, but control some values at runtime such as the startColor for the gradient. Here's what I have in my_layered_shape.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android" &gt;
    &lt;item&gt;
      &lt;shape android:shape="rectangle"&gt;
        &lt;stroke android:width="1dp" android:color="#FF000000" /&gt;
        &lt;solid android:color="#FFFFFFFF" /&gt;
      &lt;/shape&gt;
    &lt;/item&gt;
    &lt;item android:top="1dp" android:bottom="1dp"&gt; 
      &lt;shape android:shape="rectangle"&gt;
      &lt;stroke android:width="1dp" android:color="#FF000000" /&gt;
      &lt;gradient
        android:startColor="#FFFFFFFF"
        android:centerColor="#FFFFFF88"
        android:endColor="#FFFFFFFF"
        android:gradientRadius="250"
        android:centerX="1"
        android:centerY="0"
        android:angle="315"
      /&gt;            
      &lt;/shape&gt;
    &lt;/item&gt;
  &lt;/layer-list&gt;
  </code></pre>
  
  <p>And if I use mMyImageView.setBackgroundResource(R.drawable.my_layered_shape) it works.
  I don't mind splitting the xml if I have to, or doing the whole thing programatically as long as there's a way to get at the various color values. The concept I'm going for programmatically (i.e. my best shot at doing the same in code as this xml) is:</p>
  
  <pre><code>Drawable[] layers = new Drawable[2];
  
  ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
  sd1.getPaint().setColor(0xFFFFFFFF);
  sd1.getPaint().setStyle(Style.STROKE);
  sd1.getPaint().setStrokeWidth(1);
  // sd1.getPaint().somehow_set_stroke_color?
  
  ShapeDrawable sd2 = new ShapeDrawable(new RectShape());
  sd2.getPaint().setColor(0xFF000000);
  sd2.getPaint().setStyle(Style.STROKE);
  // sd2.getPaint().somehow_set_stroke_color?
  // sd2.getPaint().somehow_set_gradient_params?
  
  layers[0] = sd1;
  layers[1] = sd2;
  LayerDrawable composite = new LayerDrawable(layers);
  mMyImageView.setBackgroundDrawable(composite);
  </code></pre>
  
  <p>Thanks.</p>