title: Android Gradient drawable programmatically
body: <p>I have a gradient drawable defined in xml that I use it as a background, like this:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
     &lt;item android:bottom=&quot;4dp&quot;&gt;
        &lt;shape&gt;
           &lt;gradient
              android:startColor=&quot;@color/blue&quot;
              android:endColor=&quot;@color/dark_blue&quot;
              android:angle=&quot;270&quot; /&gt;
        &lt;/shape&gt;
     &lt;/item&gt;
     &lt;item android:top=&quot;98dp&quot;&gt;
        &lt;shape&gt;
           &lt;gradient
              android:startColor=&quot;@color/black&quot;
              android:endColor=&quot;@color/transparent_black&quot;
              android:angle=&quot;270&quot; /&gt;
        &lt;/shape&gt;
     &lt;/item&gt;
  &lt;/layer-list&gt;
  </code></pre>
  <p>I need to implement this programmatically. I have tried to use a GradientDrawable as follows (this method is implemented on a custom view):</p>
  <pre><code>int[] colors1 = {getResources().getColor(R.color.black), getResources().getColor(R.color.trasparent_black)};
  
  GradientDrawable shadow = new GradientDrawable(Orientation.TOP_BOTTOM, colors1);
  shadow.setBounds(0,98, 0, 0);
          
  int[] colors = new int[2];
  colors[0] = getResources().getColor(R.color.blue);
  colors[1] = getResources().getColor(R.color.dark_blue);
  
  GradientDrawable backColor = new GradientDrawable(Orientation.TOP_BOTTOM, colors);
  
  backColor.setBounds(0, 0,0, 4);
  
  //finally create a layer list and set them as background.    
  Drawable[] layers = new Drawable[2];
  layers[0] = backColor;
  layers[1] = shadow;
        
  LayerDrawable layerList = new LayerDrawable(layers);
  setBackgroundDrawable(layerList);
          
  </code></pre>
  <p>The problem is that it seems that setting the bounds is useless or doesn't work the same way as (android:top, android:bottom xml parameters). The resulting background is each layer painted from top to bottom, one above the other.</p>
  <p>I want to generate something like this: <a href="https://i.stack.imgur.com/9oHc6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9oHc6.png" alt="IMG" /></a></p>