Andres Andres - 4 months ago 25
Android Question

How can I set a shadow in each cell of a ListView?

I have a custom cell for an Android ListView. This custom cell is a relative layout with some views inside. There is a space between each cell, so I want to add to the bottom of the cells a shadow.

I've been googling around but couldn't found anything? I want to achieve something similar to this:

enter image description here

Thanks!

Answer

Can be done in below two ways:

  1. 9-patch images - click link for understanding 9 patch
  2. Using layer-list - Create new file in "res/drawable" which contains:

     <?xml version="1.0" encoding="utf-8"?>
       <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
         <item >
            <shape 
              android:shape="rectangle">
                  <solid android:color="@android:color/darker_gray" />
                  <corners android:radius="5dp"/>
            </shape>
         </item>
         <item android:right="1dp" android:left="1dp" android:bottom="2dp">
            <shape 
              android:shape="rectangle">
                  <solid android:color="@android:color/white"/>
                  <corners android:radius="5dp"/>
            </shape>
         </item>
       </layer-list>
    

Then add this layer-list file as background in your List item Layout(ie., Linearlayout or etc..).