55

I have a shape with a gradient that I'm using as a divider between ListView items. I've defined it as follows:

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

<gradient
    android:startColor="#ccd0d3"
    android:centerColor="#b6babd"
    android:endColor="#ccd0d3"
    android:height="1px"
    android:angle="0" />

</shape>

I would like to add 6 pixels of padding on either side of the gradient, so that it doesn't extend from edge to edge of the screen.

However, no matter where I put an android:left="6px" and android:right="6px", it doesn't seem to take effect. I can put it in the <shape> element, the <gradient> element, or in a separate <padding> child of <shape>, and it doesn't change anything.

How can I add padding on the left and right of my list divider?

emmby
  • 99,783
  • 65
  • 191
  • 249
  • I've updated all the answers to use dp not px since most of the time developers should be using dp to take into account different screen densities: http://developer.android.com/guide/practices/screens_support.html – Intrications Feb 10 '13 at 11:51
  • 2
    In addition to comments and solutions saying not to use px, use dp - I would like to add that gradient has no height attribute and is useless in this case. Height should be applied as an attribute of a element. – prodaea Mar 14 '13 at 14:46
  • Possible duplicate of [Padding doesn't affect in an XML Layout](http://stackoverflow.com/questions/1283085/padding-doesnt-affect-shape-in-an-xml-layout) – tir38 Jul 22 '16 at 17:27

3 Answers3

141

I guess you could combine it like this:

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

    <item android:left="6dp"
          android:right="6dp">

        <shape android:shape="rectangle">
            <gradient android:startColor="#ccd0d3"
                      android:centerColor="#b6babd"
                      android:endColor="#ccd0d3"
                      android:height="1px"
                      android:angle="0"/>
        </shape>
    </item>
</layer-list>
Intrications
  • 16,782
  • 9
  • 50
  • 50
anon
  • 1,411
  • 2
  • 9
  • 2
54

Another solution using inset:

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="6dp"
    android:insetRight="6dp" >

    <shape   
        android:shape="rectangle">

    <gradient
        android:startColor="#ccd0d3"
        android:centerColor="#b6babd"
        android:endColor="#ccd0d3"
        android:height="1px"
        android:angle="0" />

    </shape>

</inset>
Intrications
  • 16,782
  • 9
  • 50
  • 50
Wayne
  • 6,361
  • 10
  • 46
  • 69
23

One solution seems to be to "wrap" my drawable with another drawable that specifies the appropriate padding.

For example, list_divider.xml would be:

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

    <item
        android:left="6dp"
        android:right="6dp"
        android:drawable="@drawable/list_divider_inner" />

</layer-list>

And then list_divider_inner.xml would be the original drawable:

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

<gradient
    android:startColor="#ccd0d3"
    android:centerColor="#b6babd"
    android:endColor="#ccd0d3"
    android:height="1px"
    android:angle="0" />

</shape>

This results in two files to specify a simple divider though. I don't know if there's a way to do it with only one file though.

Intrications
  • 16,782
  • 9
  • 50
  • 50
emmby
  • 99,783
  • 65
  • 191
  • 249
  • 1
    px will look *great* an a modern xhdpi phone. — Don't use px **ever**. Use **dp** for graphics and **sp** for font sizes instead. Read: http://developer.android.com/guide/practices/screens_support.html – Martin May 02 '12 at 13:15
  • 1
    @Martin I've updated the answer to use dp to avoid copy and pasting of bad practices. – Intrications Feb 10 '13 at 11:49
  • 7
    This question was about using a fine divider between list view items. For this particular situation, i did specifically want to use a single pixel divider no matter what the resolution of the device, so px was correct. – emmby Feb 11 '13 at 01:52
  • This is the best solution i'v looking for! I'v just needed to add padding to native divider, thus I wrap `"@android:drawable/divider_horizontal_dark"` into layer-list container. Thanx! – Johnny Doe Jun 24 '13 at 15:11
  • Thanks For some reason inset doesn't work in android 4.2.2. Only this solution works. – Abhishek V Mar 21 '16 at 06:05