title: How to create Gradient with more than three colors using XML
body: <p>I need to create a linear Gradient with 5 different colors. 
  I tried the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;item&gt;
          &lt;layer-list&gt;
              &lt;item&gt;
                  &lt;shape&gt;
                      &lt;gradient
                          android:startColor="@color/diagramBlueColor"
                          android:endColor="@color/diagramGreenColor"
                          android:type="linear"
                          android:angle="0" /&gt;
                  &lt;/shape&gt;
              &lt;/item&gt;
              &lt;item&gt;
                  &lt;shape&gt;
                      &lt;gradient
                          android:startColor="@color/diagramGreenColor"
                          android:endColor="@color/diagramYellowColor"
                          android:type="linear"
                          android:angle="0" /&gt;
                  &lt;/shape&gt;
              &lt;/item&gt;
              &lt;item&gt;
                  &lt;shape&gt;
                      &lt;gradient
                          android:startColor="@color/diagramYellowColor"
                          android:endColor="@color/diagramOrangeColor"
                          android:type="linear"
                          android:angle="0" /&gt;
                  &lt;/shape&gt;
              &lt;/item&gt;
              &lt;item&gt;
                  &lt;shape&gt;
                      &lt;gradient
                          android:startColor="@color/diagramOrangeColor"
                          android:endColor="@color/diagramRedColor"
                          android:type="linear"
                          android:angle="0" /&gt;
                  &lt;/shape&gt;
              &lt;/item&gt;
          &lt;/layer-list&gt;
      &lt;/item&gt;
  &lt;/selector&gt;
  </code></pre>
  
  <p>But every shape is overriding the shape before. I need to create the gradient using xml. How can i do that?</p>
  
  <p>If it is not possible with pure xml then how can i do it in java code?
  I tried this:</p>
  
  <pre><code>/**
   *
   * @return
   */
  public static PaintDrawable getColorScala() {
      ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
          @Override
          public Shader resize(int width, int height) {
              LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                      new int[] {
                              0xFF1e5799,
                              0xFF207cca,
                              0xFF2989d8,
                              0xFF207cca }, //substitute the correct colors for these
                      new float[] {
                              0, 0.40f, 0.60f, 1 },
                      Shader.TileMode.REPEAT);
              return linearGradient;
          }
      };
  
      PaintDrawable paint = new PaintDrawable();
      paint.setShape(new RectShape());
      paint.setShaderFactory(shaderFactory);
  
      return paint;
  }
  </code></pre>
  
  <p>But when i set the background of my view:</p>
  
  <pre><code>view.setBackground(Colors.getColorScala());
  </code></pre>
  
  <p>The background of my view is white. I want it to look like that:</p>
  
  <p><a href="https://i.stack.imgur.com/c8hU4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c8hU4.png" alt="enter image description here"></a></p>