Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Gradient drawable programmatically
    primarykey
    data
    text
    <p>I have a gradient drawable defined in xml that I use it as a background, like this:</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 android:bottom="4dp"&gt; &lt;shape&gt; &lt;gradient android:startColor="@color/blue" android:endColor="@color/dark_blue" android:angle="270" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;item android:top="98dp"&gt; &lt;shape&gt; &lt;gradient android:startColor="@color/black" android:endColor="@color/transparent_black" android:angle="270" /&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="http://i49.tinypic.com/2nueqf.png">IMG http://i49.tinypic.com/2nueqf.png</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload