6

I am trying to use layer-list to create a gradient stroke (i.e. perimeter/border) as the background of my view. But it's not working.

Here is my code

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <gradient
                android:angle="270"
                android:centerColor="#FFFFFFFF"
                android:endColor="#FFCCCCCC"
                android:startColor="#FFCCCCCC"
                android:type="linear" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#F0F1F3" />

            <margin
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
        </shape>
    </item>

</layer-list>

The first item is not visible at all. only the second item fills the screen. Any thoughts on how to fix this?

Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87

2 Answers2

23

Maybe this can be of use, it create some shadow effect

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Drop Shadow Stack -->
    <item>
        <shape>
            <corners android:radius="12dp" />

            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />

            <solid android:color="#00CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <corners android:radius="12dp" />

            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />

            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <corners android:radius="12dp" />

            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />

            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <corners android:radius="12dp" />

            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />

            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <corners android:radius="12dp" />

            <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"
                android:top="1dp" />

            <solid android:color="#50CCCCCC" />
        </shape>
    </item>

    <!-- Background -->
    <item>
        <shape>
            <solid android:color="@android:color/black" />

            <corners android:radius="12dp" />
        </shape>
    </item>

</layer-list>
An-droid
  • 6,433
  • 9
  • 48
  • 93
  • Thank you so much that was helpful exactly what i wanted. – Muhammad Apr 21 '16 at 13:40
  • the questions says, _creating gradient stroke using layer-list_, but I cannot find any `gradient` in the solution code. – ArtiomLK Dec 16 '16 at 11:18
  • Yes but also : stroke (i.e. perimeter/border). That is what the answer provide. It fakes a gradient and since it's only for a stroke the eye can't see the trick. – An-droid Dec 16 '16 at 14:21
10

You can try something like this

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape
        android:innerRadius="80dp"
        android:shape="ring"
        android:thickness="20dp"
        android:useLevel="false">
        <size
            android:width="200dp"
            android:height="200dp" />
        <gradient
            android:endColor="#00ff0000"
            android:gradientRadius="200"
            android:startColor="#ffff0000"
            android:type="radial" />
    </shape>
</item>
<item
    android:bottom="20dp"
    android:left="20dp"
    android:right="20dp"
    android:top="20dp">
    <shape android:shape="oval">
        <stroke
            android:width="1dp"
            android:color="#ff0000" />
    </shape>
</item>
</layer-list>

enter image description here

efeyc
  • 2,122
  • 2
  • 27
  • 39