How to add padding to gradient <shape> in Android?

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.

  • Could not determine the class-path for interface com.android.builder.model.AndroidProject
  • How to make a ViewPager loop?
  • onClick event is not triggering | Android
  • JIT Compiler Improvements in Android 3.0 and 4.0
  • How to set property “android:drawableTop” of a button at runtime
  • Android - detect phone unlock event, not screen on
  • 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?

    Related posts:

    Shared Preferences &ldquo;limit&rdquo;
    How Intents work internally?
    Android: Where should I save temporary files?
    Seeing message in logs: &ldquo;app:theme is deprecated&rdquo;?
    Is possible use drawable-mdpi-fr, drawable-hdpi-fr, drawable-ldpi-fr, for localization with differen...
    Programmatically turn off android:windowActionBarOverlay style from action bar
  • Android maps utils cluster icon color
  • Is it possible to add any merchant account in Google wallet Android API?
  • importing correct AssertThat method for Robolectric Test
  • How can I change the width/thickness of a scrollbar?
  • How can I play a mp3 without download from the url?
  • Remove title in Toolbar in appcompat-v7
  • 3 Solutions collect form web for “How to add padding to gradient <shape> in Android?”

    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>
    

    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>
    

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.